如何在mongodb中找到匹配的记录? [英] How to Find the matched record in mongodb?

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

问题描述

我在我的收藏中有一个记录,我想获取id为1的人的详细信息。但是我得到了2倍而不是1的详细信息。

  db.mycollection.insert({person:[{id:1,details:{name:Aswini,Age:10}} ,{id:2,details:{name:Mahesh,Age:11}}]})

然后运行

 > db.mycollection.findOne({person.id:1},{person.details:1,_ id:0})

结果是:


$ person $ $ b $
$ details

$ name $ aswini
年龄10
$
$ b $ details

$ name $ $
$ Age $ 11
$ $ b}
]
}

但是我需要如下:

 $ {
$ details $ {
$ {名称:Aswini,
年龄:10






Don'不明白我们在哪里犯错误。请需要你的帮助。正在使用MongoDB,Java

解决方案

这是过滤多级嵌入式文档的行为,通常匹配过滤器会返回整个文档,而不是子集。

通常位置操作符$ 用于匹配更新。但该功能尚未在返回说明符中实现。



mongo已经有一个悬而未决的问题了支持字段中的位置($)操作符返回说明符。 (请登录投票,如果你真的需要这个功能)

所以你必须重新设计你的模式来处理这个,可能是这样的

  db.test.insert({person:[{id:1,details:{name:Aswini,Age ({person:[{id:2,details:{name:Mahesh,Age:11}):
db.test.insert }}})

db.test.find({person.id:1},{'person.details':1})




I have a record in my collection and I want to fetch the details of the person whose id is 1. But I am getting the details for 2times instead of 1.

    db.mycollection.insert({"person" : [ { "id":1, "details" : { "name" : "Aswini", "Age" : 10 }}, { "id":2, "details" : { "name" : "Mahesh", "Age" : 11}}]})

Then running

    > db.mycollection.findOne({"person.id":1},{"person.details":1,"_id":0})

the result is :

    {
        "person" : 
                [
                    {
                        "details" : 
                        {
                         "name" : "Aswini",
                         "Age" : 10
                        }
                    },
                    {
                        "details" : 
                        {
                         "name" : "Mahesh",
                         "Age" : 11
                        }
                    }
                ]
    }

But I need as follows:

    {
    "person" : [
                {
                "details" : {
                            "name" : "Aswini",
                            "Age" : 10
                        }
                }
            ]
    }

Don't understand where am making mistake. Please need your help. Am using MongoDB, Java

解决方案

This is the behavior of filtering multi level embedded document, normally the matching filter would return the whole document, not the subsets.

Usually positional operator $ used to match the sub documents in updates. But the feature is not yet implemented in return specifiers.

There is an outstanding issue already in mongo Support for positional ($) operator in fields to return specifier. (Please login to vote if you really needed the feature)

So you have to redesgin your schema to handle this, may be like this

db.test.insert({"person" : [ { "id":1, "details" : { "name" : "Aswini", "Age" : 10 }}]})
db.test.insert({"person" : [ { "id":2, "details" : { "name" : "Mahesh", "Age" : 11}}]})

db.test.find({"person.id":1},{'person.details':1})

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

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