查询嵌入列表中的数组 [英] Querying for array in embedded list

查看:66
本文介绍了查询嵌入列表中的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个与以下内容相似的mongo文档:

Supposed I have a mongo document that looks similar to the following:

{
   'foo':1
   'listOfLists' : [ [1,2],[3,4] ]
}

(是的,我知道这不是它的真正"外观,但它应该足够简单以用于说明目的.)

(Yes I am aware this isn't how it "really" looks but it should be simple enough for explanation purposes.)

如果我想编写一个查询来检查listOfLists列表对象是否包含[3,4]的组合,那我该怎么做呢?

If I wanted to write a query that would check to see if the listsOfLists list object contains the combination of [3,4], how could I go about doing that?

我可以做些类似的事情

collection.find({'listsOfLists' : {'$elemMatch' : [3,4] } })

推荐答案

collection.find({ 'listsOfLists': [3,4] }).

这只是属性上的直接匹配". MongoDB将自动查看每个数组元素.您不需要在这里 $elemMatch .

It's just a "direct match" on the property. MongoDB will look at each array element automatically. You don't need $elemMatch here.

如果要使用它,则需要一个运算符,例如 $eq :

If you were to use it, you need an operator expression, such as $eq:

collection.find({ 'listsOfLists': { '$elemMatch': { '$eq': [3,4] } } }).

但是,除非在数组元素上实际需要匹配两个或多个"条件,否则当然不需要. $elemMatch 到底是干什么的.

But that of course is not required unless there are "two or more" conditions that actually need to match on the array elements. Which is what $elemMatch is actually for.

这篇关于查询嵌入列表中的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆