0

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.

2 Answers 2

1
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));
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks its working but have one problem. Suppose i want to search todays record means fromDate : 25-09-2017 and toDate: 25-09-2017 then no result found. but if I select toDate : 26-09-2017 than the desire result comes. any idea?
you send with time means from date time 00-00-00 and to date time 23-59-59
can you please update that in your answer. That's help me to understand better.
you send like this moment('25-09-2017').format('YYYY-MM-DDT00:00:00Z');
it's my pleasure
1

I think you have the brackets mixed up. Change

db.collection(config.db.userDetailsCollection).find({logdate: {$gte:isoStartDate, $lt: isoEndDate}},{"status": "inactive"}).toArray()

to

db.collection(config.db.userDetailsCollection).find({logdate: {$gte:isoStartDate, $lt: isoEndDate}, "status": "inactive"}).toArray()

and see if it works.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.