查询MongoDb中的嵌套数组 [英] Query a nested array in MongoDb

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

问题描述

我想通过嵌套数组中字符串的存在来检索文档.例如,数据(表示句子的依存关系分析)看起来像:

I would like to retrieve documents by the presence of an string in a nested array. For example, the data (representing a dependency parse of a sentence) looks like:

{'tuples': [['xcomp', 'multiply', 'using'], 
            ['det', 'method', 'the'], 
            ['nn', 'method', 'foil'], 
            ['dobj', 'using', 'method']]}

我找到的最接近的解决方案假定['nn', ...]tuples列表列表的第二个位置:

The closest solution I've found assumes that ['nn', ...] is the second position of the tuples list-of-lists:

 db.c.find({'tuples.2.0' : 'nn'})

是否有一种方法可以放松固定位置?元组(不是它们的内容)可以是任意顺序.

Is there a way to relax the fixed position? The tuples (not their contents) can be in any order.

第二,能够检索具有['nn', 'method', X]的文档确实很棒,这意味着在其依赖项解析中有一个名词方法".

Secondly, it would be really great to be able to retrieve documents that have ['nn', 'method', X], meaning a noun "method" in their dependency parse.

谢谢!

推荐答案

知道了!

db.c.find({'tuples' : {$elemMatch : {$all : ['nn']}}})
db.c.find({'tuples' : {$elemMatch : {$all : ['nn','method']}}})

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

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