Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何实现Mongo DB查询以在数组中的所有对象中查找键?_Mongodb_Meteor_Spacebars - Fatal编程技术网

Mongodb 如何实现Mongo DB查询以在数组中的所有对象中查找键?

Mongodb 如何实现Mongo DB查询以在数组中的所有对象中查找键?,mongodb,meteor,spacebars,Mongodb,Meteor,Spacebars,在查阅了几本书、mongo db参考资料和其他stackoverflow问题之后,我似乎无法正确回答这个问题 我有以下数据结构: 职位收集: { "content" : "...", "comments" : [ { "author" : "joe", "score" : 3, "comment" : "nice post" }, {

在查阅了几本书、mongo db参考资料和其他stackoverflow问题之后,我似乎无法正确回答这个问题

我有以下数据结构:

职位收集:

{
    "content" : "...",
    "comments" : [
        {
            "author" : "joe",
            "score" : 3,
            "comment" : "nice post"
        }, 
        {
            "author" : "bob",
            "score" : 5,
            "comment" : "nice"
        }]
}
我试图获取的是Handlebar helper中数组的每个对象中的所有作者名,而不是控制台中的所有作者名。所以我想说:

...
 commentsAuthors: function() {
  return Collection.find({...});
 }
更新: 我决定将我的数组减少为一个字符串数组,以后我会这样查询:

新阵列:

{
    "content" : "...",
    "comments" : ["value1", "value2", "..."]
}
MeteorJS车把辅助工具:

Template.courseEdit.helpers({
 comments: function(){
  var cursor = Courses.find({"_id": "8mBATtGyyWsGr2PwW"}, {"comments": 1, "_id": 0}).fetch();

 return cursor.map(function(doc, index, cursor){
   var comment = doc.comments;
   console.log(comment);
   return comment;
  });
 }
});
此时,我可以用这个
{{{{each}}…{{/each}
在我的视图中呈现整个数组:

<ul class="list-unstyled">
   {{#each comments}}
     <li class="pl-14"><i class="icon-checkmark"></i> {{this}}</li>
   {{/each}}
</ul>
    {{{#每个评论}
  • {{this}
  • {{/每个}}
但是,我在一个列表项中获得了整个数组

如何为每个数组字符串创建单独的列表项


提前感谢。

通过限制字段,您只能获得某些字段

 db.collection.find({"content" : "..."},{"comments.author":1})
应尽可能简单(用您的收藏名称替换
posts
):

或者,如果您需要每篇文章:

db.posts.aggregate([
  { $unwind: "$comments" }, 
  { $group: { "_id": "$_id", authors: { "$addToSet": "$comments.author"} } } 
])

感谢您的帮助,不过我正在尝试特别获取
注释
数组中对象键的所有值。你知道怎么做吗?注意:在Handlebars助手中,而不是在控制台中。感谢您的帮助,但是我正在尝试专门获取comments数组中对象的键的所有值。你知道怎么做吗?注意:在把手辅助工具中,而不是在控制台中。
db.posts.aggregate([
  { $unwind: "$comments" }, 
  { $group: { "_id": "$_id", authors: { "$addToSet": "$comments.author"} } } 
])