Mongodb:使用多个条件查找对象

时间:2017-09-25 05:36:06

标签: node.js mongodb

这可能是一个非常基本的问题,但我发现很难得到它。我想找到所有" logdate"介于日期范围和状态"之间没有用。

这是我的数据:

{
        "_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"
}

这是我的疑问:

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()

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:1)

我认为你的括号已经混淆了。变化

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

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

看看它是否有效。

答案 1 :(得分: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));