如何从 mongoDB 检索任何元素值?

发布于 2024-12-28 18:32:40 字数 473 浏览 0 评论 0原文

假设我有以下集合:

{    _id" : ObjectId("4f1d8132595bb0e4830d15cc"), 
    "Data" : "[
            { "id1": "100002997235643", "from": {"name": "Joannah" ,"id": "100002997235643"} , "label" : "test"  } , 
            { "id1": "100002997235644", "from": {"name": "Jon" ,"id": "100002997235644"} , "label" : "test1"  } 
        ]" , 
    "stat" : "true" 
}

如何检索 id1 、 name 、 id 、label 或任何其他元素?

我能够获取 _id 字段、DATA(完整数组),但不能获取 DATA 中的内部元素。

Suppose I have following collection :

{    _id" : ObjectId("4f1d8132595bb0e4830d15cc"), 
    "Data" : "[
            { "id1": "100002997235643", "from": {"name": "Joannah" ,"id": "100002997235643"} , "label" : "test"  } , 
            { "id1": "100002997235644", "from": {"name": "Jon" ,"id": "100002997235644"} , "label" : "test1"  } 
        ]" , 
    "stat" : "true" 
}

How can I retrieve id1 , name , id ,label or any other element?

I am able to get _id field , DATA (complete array) but not the inner elements in DATA.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青衫负雪 2025-01-04 18:32:40

您无法查询嵌入结构。您总是查询顶级文档。如果要查询数组中的各个元素,则必须将这些元素制作为顶级文档(因此,将它们放入自己的集合中)并在此文档中维护一个 _ids 数组。

也就是说,除非数组变得非常大,否则简单地获取整个文档并在应用程序中找到适当的元素几乎总是更有效。

You cannot query for embedded structures. You always query for top level documents. If you want to query for individual elements from your array you will have to make those element top level documents (so, put them in their own collection) and maintain an array of _ids in this document.

That said, unless the array becomes very large it's almost always more efficient to simply grab your entire document and find the appropriate element in your app.

悲歌长辞 2025-01-04 18:32:40

我认为你做不到。 此处进行了解释

如果您想访问特定字段,请按照 MongoDB 文档
您可以向查询添加一个标志参数,但您应该重新设计文档才能使其发挥作用:

字段选择

除了查询表达式之外,MongoDB 查询还可以采用一些其他参数。例如,可以请求仅返回某些字段。如果我们只想要姓氏为“Smith”的用户的社会安全号码,那么我们可以从 shell 发出以下查询:

// retrieve ssn field for documents where last_name == 'Smith':
db.users.find({last_name: 'Smith'}, {'ssn': 1});

// retrieve all fields *except* the thumbnail field, for all documents:
db.users.find({}, {thumbnail:0});

I don't think you can do that. It is explained here.

If you want to access specific fields, then following MongoDB Documentation,
you could add a flag parameter to your query, but you should redesign your documents for this to be useful:

Field Selection

In addition to the query expression, MongoDB queries can take some additional arguments. For example, it's possible to request only certain fields be returned. If we just wanted the social security numbers of users with the last name of 'Smith,' then from the shell we could issue this query:

// retrieve ssn field for documents where last_name == 'Smith':
db.users.find({last_name: 'Smith'}, {'ssn': 1});

// retrieve all fields *except* the thumbnail field, for all documents:
db.users.find({}, {thumbnail:0});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文