从 mongoDB 数组中获取特定元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5562485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 12:02:56  来源:igfitidea点击:

Get particular element from mongoDB array

arraysmongodb

提问by Swapnil Sonawane

I have mongo collection like below

我有如下 mongo 集合

{
  "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}
    ]
}

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

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

i have make query like below

我有如下查询

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

but it's gives output as follows

但它给出的输出如下

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

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

我想获得书籍 b1 和其他 xyz 的唯一日期。 "books" : {"date" : 2-4-00}

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

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

采纳答案by Gates VP

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

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

There is no support for returning only sub-documents.

不支持仅返回子文档。

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

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



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

更新:看起来票已被标记为固定。

See herefor an example of how to use this.

有关如何使用它的示例,请参见此处

回答by bm_i

It can be done using map/reduce, just emit the sub element into a temporary inline collection. Its a Hack and it works however I'd advise against it as map/reduce is single threaded, and has a large overhead for what you want to achieve, it is much easier to just extract the sub-element in your application.

可以使用 map/reduce 来完成,只需将子元素发送到临时内联集合中即可。它是一个 Hack 并且它可以工作,但是我建议不要使用它,因为 map/reduce 是单线程的,并且对于您想要实现的目标有很大的开销,在您的应用程序中提取子元素要容易得多。

Something like this...

像这样的东西...

map:

地图:

m = function() { 
    this.books.forEach(function(book){ 
        if(book1 == 'b1'){
           emit("books", {date: book.date,});
         }
     });
}

reduce:

降低:

r = function(key, values) {
      return this;
    }

query:

询问:

    db.coll.mapReduce(m, r, {query : {"auther" : "xyz" , "books.book1" : "b1"}, out: { inline : 1}})

回答by Swapnil Sonawane

if you want to select only matching element you can query like this.

如果您只想选择匹配的元素,您可以这样查询。

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

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

回答by Jason Sebring

With a little imagination (pre mongo v 2.6) ...

有点想象力(pre mongo v 2.6)......

You can do this with aggregate or map reduce. Aggregate is newer, easier and more optimized. Here's a sample of returning a sub document with aggregate assuming your collection is named "Authors". I took the liberty of spelling things correctly.

您可以使用聚合或映射缩减来做到这一点。聚合更新、更简单、更优化。这是返回带有聚合的子文档的示例,假设您的集合名为“作者”。我冒昧地拼写正确。

Authors.aggregate([
  { $match: { author: 'xyz' } },
  { $unwind: '$books' },
  { 
    $project: {  
      _id: '$books.book1',
      date: '$books.date'
    }
  },
  { $match: { '$_id' : 'b1' } } 
]);

You'll get back an array with a single entry like so:

您将返回一个包含单个条目的数组,如下所示:

[{ _id: 'b1', date: '2-4-00' }]

Otherwise, if mongo 2.6+you can do the really easy way:

否则,如果mongo 2.6+你可以做一个非常简单的方法:

Authors.find({
  author: 'xyz',
  books: { $elemMatch: { book1: 'b1' } }
},'books')

Where you will get back the books collection if found and only a single record within:

如果找到并且只有一个记录,您将在哪里取回书籍收藏:

{ _id: 'xyz', books: [ { book1: 'b1', date: '2-4-00' } ] }