在 MongoDB 中,如何找到嵌入的文档? [英] In MongoDB, how can I find a document that is embedded?

查看:64
本文介绍了在 MongoDB 中,如何找到嵌入的文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个名为KPOP"的嵌入文档.在MongoDB中,我想找到值为BTS:Fake Love"的字段

I created an embedded document named "KPOP" in MongoDB, and I wanted to find the field with the value of "BTS: Fake Love"

这是嵌入的文档:

> use TestMongoDB
  switched to db TestMongoDB
> db.KPOP.find().pretty()
{
    "_id" : ObjectId("5fd83447066c904c1365771a"),
    "KPOP" : {
            "SHINee" : "Dream Girl",
            "Girl's Generation" : "I Got a Boy",
            "BTS" : "Fake Love"
    }
} 

我的代码有问题吗?按回车后,什么都没有显示.

Is something wrong with my code? After I press enter, nothing shows up.

> db.KPOP.find( {"KPOP": {"BTS": "Fake Love"}} )

推荐答案

我的代码有问题吗?按回车后,什么都不显示

Is something wrong with my code? After I press enter, nothing shows up.

db.KPOP.find( {"KPOP": {"BTS": "Fake Love"}})

db.KPOP.find( {"KPOP": {"BTS": "Fake Love"}} )

这是预期的行为.

如果您想查询嵌入文档的单个字段,您需要使用以下语法 - 使用点 (.) 表示法.例如,这两个查询都返回文档:

If you want to query an embedded document's individual field(s) you need to use the following syntax - using the dot (.) notation. For example, both these queries return the document:

db.test.find({"KPOP.SHINee": "Dream Girl"})
db.test.find({"KPOP.SHINee": "Dream Girl", "KPOP.BTS": "Fake Love" })

在上述查询中,您可以按任意顺序指定一个、两个或所有字段.

In the above queries, you can specify one, two or all the fields and in any order.

在为整个嵌入文档指定过滤器时,您使用以下语法:

You use the following syntax when specifying the filter for the whole embedded document:

db.test.find({ KPOP: { SHINee: "Dream Girl", "Girl's Generation": "I Got a Boy", BTS: "Fake Love" } })

在这种情况下,请注意嵌入文档的字段顺序必须与原始文档的字段顺序相同;即,SHINeeGirl's Generation"BTS(在 KPOP 内).并且,您需要指定所有嵌入文档的字段.

In this case, note that the order of the fields of the embedded document must be same as that of the original document; i.e., SHINee, "Girl's Generation" and BTS (within the KPOP). And, you need to specify all the fields of the embedded document.

这篇关于在 MongoDB 中,如何找到嵌入的文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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