Javascript 查找数组中可能存在或不存在的元素
我有一个文档看起来有点像这样:Javascript 查找数组中可能存在或不存在的元素,javascript,mongodb,Javascript,Mongodb,我有一个文档看起来有点像这样: > db.orders.find() { _id: ObjectId(), _reminders: [{ notified: true, timestamp: ISODate(), completed: false }] } { _id: ObjectId(), _reminders: [] } 我试图找到的是订单集合中的一个文档,“提醒”在特定时间范围内不包含提醒,并且未完成 db.orders.find({
> db.orders.find()
{
_id: ObjectId(),
_reminders: [{
notified: true,
timestamp: ISODate(),
completed: false
}]
}
{
_id: ObjectId(),
_reminders: []
}
我试图找到的是订单集合中的一个文档,“提醒”在特定时间范围内不包含提醒,并且未完成
db.orders.find({
'_reminders': {
$elemMatch: {
completed: false,
timestamp: { $ne: time }
}
}
});
问题是,这将找不到一个没有任何提醒的订单
如何查询这个问题?您应该使用查询
db.orders.find({$or:[{{u提醒:[]},这里放上您的查询与时间匹配]})
-它将返回与您的查询匹配的文档和带有空提醒的文档您应该使用查询
db.orders.find({$or:[{{u提醒:[]},这里放上你的查询与时间匹配]})
-它将返回与你的查询匹配的文档和带有空提醒的文档这应该能满足你的需要
db.getCollection('Clock').find({
$or : [
{
_reminders : {
$elemMatch : {
timestamp : {
$lte : ISODate("2019-07-12T15:35:32.278Z"),
$gte : ISODate("2012-07-12T15:35:32.278Z")
},
completed : false
}
}
},
{
_reminders : {$size : 0}
},
{
_reminders : {$exists : false}
}
]
})这会让你得到你想要的
db.getCollection('Clock').find({
$or : [
{
_reminders : {
$elemMatch : {
timestamp : {
$lte : ISODate("2019-07-12T15:35:32.278Z"),
$gte : ISODate("2012-07-12T15:35:32.278Z")
},
completed : false
}
}
},
{
_reminders : {$size : 0}
},
{
_reminders : {$exists : false}
}
]
})