从 mongoDB 数组中获取特定元素 [英] Get particular element from mongoDB array

查看:114
本文介绍了从 mongoDB 数组中获取特定元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像下面这样的 mongo 集合

I have mongo collection like below

{
  "auther" : "xyz" , 
  "location" : "zzz" , 
  "books" : 
    [
      {"book1" : "b1" , "date" : 2-3-00} ,
      {"book1" : "b2" , "date" : 4-9-00}
    ]
}

{
  "auther" : "pqr",
  "location" : "zzz" , 
  "books" : 
    [
      {"book1" : "b1" , "date" : 2-4-00}
    ]
}

我想获得书籍 b1 和作者 xyz 的唯一日期.

I want to get the only the date of book b1 and author xyz .

我有如下查询

db.coll.find({"auther" : "xyz" , "books.book1" : "b1"} , {"books.date" : 1})

但它的输出如下

"books" : {"date" : 2-4-00} , "books" : {"date" : 4-9-00}

我想得到书 b1 和其他 xyz 的唯一日期.意味着只有 "books" : {"date" : 2-4-00}

I want to get the only the date of book b1 and other xyz .means only "books" : {"date" : 2-4-00}

在 mongo 中是可能的还是我做错了什么?

is it possible in mongo or am I doing something wrong?

推荐答案

MongoDB 查询语言旨在返回所有匹配的文档.

The MongoDB query language is designed to return all matching Documents.

不支持仅返回子文档.

此问题在 MongoDB 的票务跟踪器中有一个未清票.

This issue has an outstanding ticket in MongoDB's ticket tracker.

更新:该工单似乎已被标记为已修复.

UPDATE: it looks like the ticket has been marked as fixed.

请参阅此处了解如何使用此功能的示例.

See here for an example of how to use this.

这篇关于从 mongoDB 数组中获取特定元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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