MongoDB:匹配数组中的非空文档 [英] MongoDB: match non-empty doc in array
本文介绍了MongoDB:匹配数组中的非空文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样构成的集合:
I have a collection structured thusly:
{
_id: 1,
score: [
{
foo: 'a',
bar: 0,
user: {user1: 0, user2: 7}
}
]
}
我需要找到所有具有至少一个分数"(分数数组中的元素)且具有特定值"bar"的文件,以及一个非空的用户"子文档.
I need to find all documents that have at least one 'score' (element in score array) that has a certain value of 'bar' and a non-empty 'user' sub-document.
这是我想出的(似乎应该可以使用):
This is what I came up with (and it seemed like it should work):
db.col.find({score: {"$elemMatch": {bar:0, user: {"$not":{}} }}})
但是,我收到此错误:
error: { "$err" : "$not cannot be empty", "code" : 13030 }
还有其他方法吗?
推荐答案
弄清楚了:{ 'score.user': { "$gt": {} } }
将匹配非空文档.
Figured it out: { 'score.user': { "$gt": {} } }
will match non-empty docs.
这篇关于MongoDB:匹配数组中的非空文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文