I am trying to fetch the latest data from MongoDB in nodejs. let me explain with an example every 2hours I am uploading the data to a collection. when I do a get method I need the latest data how do I do that.
Please help me with this issue.
Here is the Schema
var abc = new mongoose.Schema({
ItemName : String,
date: Date
......
});
This is how I am storing
finalData.push({
ItemName: xyz,
date: new Date()})
abc.insertMany(finalData)
try like this:
your_model_name.find({}).sort('-date').exec(function(err, docs) { ... });
you can sort your collection and get the desired document by limiting the document size to one.
abc.find({}).sort([['date', -1]]).limit(1).exec(function(err, doc) { });
abc.find({}).sort('date').limit(1).then(data=>console.log(data))
criteria can be asc, desc, ascending, descending, 1, or -1
abc.find({}).sort({ field : criteria}).limit(1).exec(function(err, data){ });
Related
I want to delete X number of posts starting from the most recently created for a specific user.
How can I apply this logic using mongoose so that I can perform just ONE database operation instead of first having to query for these posts and remove them one by one?
I am finding using the query and projection operators with the $ very confusing, any help would be appreciated.
Below I added pseudo code on how it should to work.
Thank you!
const userId = "123456"
const deleteCount = 6
const deletedPosts = await Post.deleteMany({
.where { userid == userId) }
.sort({createdAt: -1}) // delete by most recent
.limit(deleteCount) // only delete 6 (most recent) posts
}, {new: true}) // return results of operation
console.log(deletedPosts.deletedCount) // -> should be "6"
You can't set a limit when using deleteMany or findAndModify. So, if you want to precisely limit the number of documents removed, you'll need to do it in two steps.
db.getCollection('users').find({}, {class : 4}) .limit(2) .sort({StudentAge: -1}) .toArray() .map(function(doc) { return doc._id; }); //returns the array of id's
db.getCollection('users').deleteMany({_id: {$in: [ "s3", "s4" ]}})//put the array of id's
I have a postgres server running with one column (say marks) of type VARCHAR(255), but is supposed to have numbers, like if i do a select *.. query , i will get ['100','50','21','14'...] etc.
i would like to run a range query on it, like user passes [10,30] and gets ['21','14'] as result. I think this would require casting at the time of running the BETWEEN query, but i cannot get it to work properly.
I am using sequalize.js which is generating the following query:
SELECT "id"
FROM "token_attributes" AS "token_attributes"
WHERE "token_attributes"."attributesDirectoryId" = 3
AND CAST('token_attributes.attributeValue' AS INTEGER) BETWEEN 10 AND 30;
on server also this query seems to fail. the sequalize query that is being created is :
{
where: {
attributesDirectoryId: 3,
attributeValue: Where { attribute: [Cast], comparator: '=', logic: [Object] }
},
attributes: [ 'id' ]
}
i have used the following code to create the where condition (cast and where were imported from sequelize):
let whereFilter ={}
let value = where(cast(`${tableName}.attributeValue`, 'integer'), {[Op.between]: rangeAsInt})
whereFilter['attributeValue'] = value
so this is basically calling table.findAll({where:whereFilter}) I am not sure how to either make sequelize create a correct sql api or what the actual correct SQL api would be. can anyone help?
found the issue, i missed the sequilize.col function :
let whereFilter ={}
let value = where(cast(col(`${tableName}.attributeValue`), 'integer'), {[Op.between]: rangeAsInt})
whereFilter['attributeValue'] = value
and the query would be :
SELECT "id"
FROM "token_attributes" AS "token_attributes"
WHERE "token_attributes"."attributesDirectoryId" = 3
AND CAST("token_attributes"."attributeValue" AS INTEGER) BETWEEN 10 AND 30;
Suppose I have a MySQL table which looks like this:
Each job in the table contains 3 tasks.
How can I get all the JobIds whose taskA is in Done state and taskB is in New state?
In my case, I want a query which returns qwert, and zxcv.
I've come up with this query:
select JobId from MyTable where TaskSeq=0 and TaskState='Done'
intersect
select JobId from MyTable where TaskSeq=1 and TaskState='New';
but my version of MySQL doesn't support the intercept operator.
My ultimate goal is to write the query in sequelize. But I think I should know the MySQL query first so that I can create a sequlize query.
And I also wish that the sequlize query can be done in 1 function instead of multiple functions concatenated with then.
Here's the SQL Fiddle to help you try the table.
You could just use a join:
select mt.JobId
from MyTable mt
join MyTable mt2 on mt2.JobId = mt.JobId
where mt.TaskSeq = 0 and mt.TaskState = 'Done' and mt2.TaskSeq = 1 and mt2.TaskState = 'New'
Here's an attempt at Sequelize on this query, however this is a guess. Hopefully it gives you something to work with:
MyTable.findAll({
attributes: ['JobId', 'TaskSeq', 'TaskState'],
include: [{
model: MyTable,
attributes: ['JobId', 'TaskSeq', 'TaskState'],
where: {
JobId: Sequelize.col('MyTable.JobId'),
TaskSeq: 1,
TaskState: 'New'
}
}],
where {
TaskSeq: 0,
TaskState: 'Done'
}
});
I`m using bookshelf.js and I want to change the data type column, this is the example I want to do.
select * from table where date(datetime_col) = '2017-03-14'
the datatime_col is DATETIME and I want to convert to date to execute the query.
this is how I'm trying to do in bookshelf
var Model = new model().query(function (qb) {
qb.where('date(datetime_col)', '=' , date);
}).fetchAll()
this is the error when I´m trying to execute the code above
Error: ER_BAD_FIELD_ERROR: Unknown column 'date(datetime_col)' in 'where clause'
This is a full example how can you use knex.raw and bookshelf for query thanks for answer.
Using between two dates with date time column
var Model = new model().query(function (qb) {
qb.whereBetween( Bookshelf.knex.raw("DATE(colum_datetime)"), [date_var1, date_var2]);
}).fetchAll( { withRelated: ['table1', 'table2', {'table3':function (qb) {
qb.orderBy('date_column', 'desc')
}},'table4'] });
Using where
var Model = new model().query("where", Bookshelf.knex.raw("DATE(colum_datetime)"), "=", date_var).fetchAll( { withRelated: ['table1', 'teble2', {'table3':function (qb) {
qb.orderBy('date_column', 'desc')
}},'table4'] });
It looks like that "bookshelf.js" or the underlying "knex" expect to get an attribute name of your model (like datetime_col), and not a sql-fragment like date(datetime_col).
According to this source, you need to pass a part with the function as "raw" sql.
The proposed solution is to write it like this:
model.query("where", Bookshelf.knex.raw("DATE(field) = ?"), "2017-03-04"))
{"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"emaple1#gmail.com","last_login":"11:25:24 AM","name_surname":"user1","password":"qwerty123"}
{"__v":0,"_id":{"$oid":"55ef49dd5d610eab18719deb"},"admin":true,"email":"emaple2#gmail.com","last_login":"12:25:24 AM","name_surname":"user2","password":"qwerty123"}
{"__v":0,"_id":{"$oid":"55f0173bb3322bf560724fd1"},"admin":true,"email":"emaple3#gmail.com","last_login":"10:25:24 AM","name_surname":"user3","password":"qwerty123"}
Hello, I working in nodeJS file and there I have a collection of JSON objects and I would like to make a search through it. For each user from this list I need to compare the field "last_login" .
I am new to nodeJS can someone help? Thank you!
This is what i have tried:
User.find({}, {last_login: }, function(err, docs) {
if (err) {
return res.json({ success: false, message: 'Failed to display last logins.' });
}
docs.forEach(function(doc, index) {
res.json({success: true, message: 'time of last login', last_login: doc.last_login});
})
//res.json(users);
});
});
Where last_login is a field in the User object and basically I need to iterate over all users in the db and extract only the last_login and display in in the response.I don’t know what value to put in the find() inside the curly braces
this is the part where I am stuck
I’ve slightly changed the function and it returns a JSON object containing the info about one user that is matched with the search query. The problem is, the console displays the result, as a whole object, although I want to get only a specific key value pair and namely last_login: value
function searchByUserName(name_surname) {
return list.filter(function(user) {
return user.name_surname === name_surname;
});
}
var a = searchByUserName('user1');
for (last_login in a ) {
if (a.hasOwnProperty(last_login)) {
console.log("last_login" + "=" + JSON.stringify(a[last_login]))
}
}
Can you tell me please, what change to make in order to get only the last_login key
here is a sample result from the console.log() that I receive:
last_login={"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"emaple1#gmail.com","last_login":"11:25:24 AM","name_surname":"user1","password":"qwerty123"}
although I want last_login = “last_login”: 11:25:24 AM
Assuming it's an array of objects like bellow.
var users = [{"__v":0,"_id":{"$oid":"55f13d34258687e0bb9e4385"},"admin":true,"email":"emaple1#gmail.com","last_login":"11:25:24 AM","name_surname":"user1","password":"qwerty123"},
{"__v":0,"_id":{"$oid":"55ef49dd5d610eab18719deb"},"admin":true,"email":"emaple2#gmail.com","last_login":"12:25:24 AM","name_surname":"user2","password":"qwerty123"},
{"__v":0,"_id":{"$oid":"55f0173bb3322bf560724fd1"},"admin":true,"email":"emaple3#gmail.com","last_login":"10:25:24 AM","name_surname":"user3","password":"qwerty123"}];
you can create a function like bellow
function searchByLastLogin(last_login) {
return users.filter(function(user) {
return user.last_login === last_login;
});
}
console.log(searchByLastLogin("12:25:24 AM"));
console.log(searchByLastLogin("10:25:24 AM"));
console.log(searchByLastLogin("11:25:24 AM"));
It will retrun a array of users whose last_login will match to given parameter last_login.
Update
What I understood from your comment bellow, you want last logins of every user.
For that you can do something like bellow
var last_logins = users.map(function(user){
return user.last_login;
});
console.log(last_logins); //prints [ '11:25:24 AM', '12:25:24 AM', '10:25:24 AM' ]
References
filter | map
I don’t know what value to put in the find() inside the curly braces this is the part where I am stuck
If I understand correctly, you only want to get the last_login field for the user model, and that's what you're struggling with ?
According to the documentation, this should work if you only want to get that field :
User.find({}, {last_login:1, _id:0})