我的Mongo数据是这样的
{
"_id" : "5a5ffef0869422169c7aa01b",
"email" : "[email protected]",
"checkin" : "2018-2-28 5:12",
"checkout" : "2018-2-28 6:12",
}
我有2个数组:
checkin : ['2018-2-28 5:12','2018-2-28 5:13','2018-2-28 5:14']
checkout : ['2018-2-28 6:12','2018-2-28 6:13','2018-2-28 6:14']
我的查询找到email
。
db.getCollection('test').find({checkin : {$in: checkin}}, {"email": 1, "_id": 0})
我得到了结果
[email protected] [email protected]
现在,我遇到了一个问题。我想找到email
得到checkin
和checkout
。这意味着只发现email
在checkin - checkin array
和checkout - checkout array
有价值
我试过这个查询但不行
db.getCollection('test').find({checkin : {$in: checkin}},{checkout : {$in: checkout}}, {"email": 1, "_id": 0})
db.getCollection('test').find(
{ $and: [
{'checkin' : {$exists: true}},
{'checkout' : {$exists: true}}
]
},
{
'email':1
//just set email:1 to get only email in projection
})
如果'checkin'和'checkout'两个数组都存在,此查询将返回电子邮件。以防您想要检查可以使用的数组大小
'checkin':{ $size:{ $gt:1 } }