A. Paul A. Paul - 24 days ago 9
Node.js Question

Mongodb : Find objects using multiple conditions

This is probably a very basic question but I am find difficulty to get it properly. I want to find all objects which "logdate" is between a date range and which "status" is inactive.

This is my data looks like:

{
"_id" : ObjectId("59c359b310c4af1b68e4175f"),
"instanceId" : "53871505974705410",
"socketId" : "jZWhIjVP6eCfrCy5AAAO",
"FName" : "",
"MName" : "",
"LName" : "",
"userToken" : "",
"EmailId" : "",
"MobileNo" : "",
"status" : "inactive",
"logdate" : ISODate("2017-09-21T06:18:27.181Z"),
"EnteredName" : "Raj"
}
{
"_id" : ObjectId("59c366ff5601022638484dbc"),
"instanceId" : "515421505978110987",
"socketId" : "vk-dB-b0GLxpj0b5AAAD",
"FName" : "",
"MName" : "",
"LName" : "",
"userToken" : "",
"EmailId" : "",
"MobileNo" : "",
"status" : "active",
"logdate" : ISODate("2017-09-21T07:15:11.823Z")
}
{
"_id" : ObjectId("59c88824651d770ec46b95d7"),
"instanceId" : "22631506314272112",
"socketId" : "ceS0MPxaB78eXTdMAAAC",
"FName" : "",
"MName" : "",
"LName" : "",
"userToken" : "",
"EmailId" : "",
"MobileNo" : "",
"status" : "inactive",
"logdate" : ISODate("2017-09-25T04:37:56.868Z"),
"EnteredName" : "abir"
}


and this is my query:

var isoStartDate = 2017-09-24T18:30:00.000Z
var isoEndDate = 2017-09-24T18:30:00.000Z
db.collection(config.db.userDetailsCollection).find({logdate: {$gte:isoStartDate, $lt: isoEndDate}},{"status": "inactive"}).toArray()


any help would be very much appreciated.

Answer Source
var queryObj = {};

if (req.query.from_date && req.query.to_date) {
    var date = {};
    var fromDate = new Date(req.query.from_date);
    var toDate = new Date(req.query.to_date);
    date.$gte = fromDate;
    date.$lte = toDate;
    queryObj.logdate = date;
}

queryObj.status = "inactive";

db.collection(config.db.userDetailsCollection).find(queryObj,function(err,result));