使用逻辑条件AND查找或查询Mongodb数据

问题描述 投票:1回答:1

我的Mongo数据是这样的

{
    "_id" : "5a5ffef0869422169c7aa01b",
    "email" : "[email protected]", 
    "checkin" : "2018-2-28 5:12",
    "checkout" : "2018-2-28 6:12",
}

我有2个数组:

  1. checkin : ['2018-2-28 5:12','2018-2-28 5:13','2018-2-28 5:14']
  2. 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得到checkincheckout。这意味着只发现emailcheckin - checkin arraycheckout - checkout array有价值

我试过这个查询但不行

db.getCollection('test').find({checkin : {$in: checkin}},{checkout : {$in: checkout}}, {"email": 1, "_id": 0})
mongodb
1个回答
1
投票
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 } } 
© www.soinside.com 2019 - 2024. All rights reserved.