MongoDB中找到多个数组项 [英] mongodb find by multiple array items

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

问题描述

如果我有一个纪录是这样;

If I have a record like this;

{
  "text": "text goes here",
  "words": ["text", "goes", "here"]
}

我怎样才能匹配来自它在MongoDB中多话?当匹配一个字我可以做到这一点;

How can I match multiple words from it in MongoDB? When matching a single word I can do this;

db.find({ words: "text" })

但是,当我尝试一下本作多个字,这是行不通的;

But when I try this for multiple words, it doesn't work;

db.find({ words: ["text", "here"] })

我猜,通过使用一个数组,它试图对一整个阵列与记录相符,而不是单个的内容相匹配。

I'm guessing that by using an array, it tries to match the entire array against the one in the record, rather than matching the individual contents.

推荐答案

取决于是否你想查找文档,其中包含两个元素(文本此处)使用的 $所有

Depends on whether you're trying to find documents where words contains both elements (text and here) using $all:

db.things.find({ words: { $all: ["text", "here"] }});

或其中一方(文本此处)使用的 $在

or either of them (text or here) using $in:

db.things.find({ words: { $in: ["text", "here"] }});

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

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