0

when I query the database with "$gte" and "$lte",Returned some data that did not match, which made me very confused.Please help me~~~ thank you!!!

The data I expected loginTime is Between 1535527571405 and 1535527571405 But the result is not consistent

this is my mongodb script and search result:

db.getCollection("userLog").find({ "loginlogs.loginTime" : {"$gte" : 1535527571405, "$lte" : 1535527571420}}, {'loginlogs.loginTime': 1})

result.png

em~ my db version is v.3.3.11

surpass
  • 1
  • 1
  • Use `db.userLog.aggregate([ {$match: { "loginlogs": { $elemMatch: { loginTime: { $gte: 1535527571405, $lte: 1535527571420 } } } }}, {$project: { loginTime: {$filter: { input: '$loginlogs.loginTime', cond: {$and:[{$gte: ['$$this', 1535527571405]}, {$eq: ['$$this', 1535527571420]}]} }}, _id: 0 }} ])` – s7vr Aug 30 '18 at 14:14
  • Sorry, my question is not clearly described. I want to get the original data that meets the criteria. According to the clues you provided, I solved the problem by executing the following script. `db.getCollection("userLog").find({loginlogs: {$elemMatch: {loginTime: {"$gte" : 1535527571405, "$lte" : 1535527571420}}}})` – surpass Aug 31 '18 at 02:06
  • No worries. Glad you resolved the issue. – s7vr Aug 31 '18 at 02:09

2 Answers2

0

Seeing your result, loginLogs.loginTime is an array, so your query will return all userLog, which have a corresponding loginTime value in loginLogs array. To achieve your needs, you can use aggregation framework :

DATASET :

{ 
    "_id" : ObjectId("5b87f33d9dc5882db7dd81fa"), 
    "loginLogs" : [
        {
            "loginTime" : 152.0
        }, 
        {
            "loginTime" : 167.0
        }, 
        {
            "loginTime" : 183.0
        }, 
        {
            "loginTime" : 192.0
        }, 
        {
            "loginTime" : 202.0
        }
    ]
}
{ 
    "_id" : ObjectId("5b87f3519dc5882db7dd81fb"), 
    "loginLogs" : [
        {
            "loginTime" : 154.0
        }, 
        {
            "loginTime" : 161.0
        }, 
        {
            "loginTime" : 178.0
        }, 
        {
            "loginTime" : 194.0
        }, 
        {
            "loginTime" : 198.0
        }
    ]
}

QUERY:

db.logs.aggregate(
    [
        {
            $unwind: {
                path : "$loginLogs",

            }
        },
        {
            $match: {
            "loginLogs.loginTime":{$gte:170,$lt:195}
            }
        },
        {
            $group: {
            _id:"$_id",
            loginLogs:{$push:"$loginLogs.loginTime"}
            }
        },
    ],
);

will output :

{ 
    "_id" : ObjectId("5b87f3519dc5882db7dd81fb"), 
    "loginLogs" : [
        178.0, 
        194.0
    ]
}
{ 
    "_id" : ObjectId("5b87f33d9dc5882db7dd81fa"), 
    "loginLogs" : [
        183.0, 
        192.0
    ]
}
matthPen
  • 4,253
  • 1
  • 16
  • 16
0

you may prefer using filter stage of aggregation https://docs.mongodb.com/manual/reference/operator/aggregation/filter/

richi arora
  • 273
  • 4
  • 11