2017-01-05 10 views
0

私は自分のニーズに顕微鏡のチュートリアルを適応させていますが、投稿内に埋め込まれているすべてのコメントを集めるクエリを書くのが難しいです。ここでMongo - 投稿の質問にすべての埋め込みコメントをリクエスト

は、これまでのところ、私のクエリです: ここPosts.find({"postId": this._id}, {"comments":{}});

は、例えば、ポストは、私からのコメントを取得したいです:

{ 
"_id": "Ad9RYqWqbsJKZx3h7", 
    "title": "Writing decimal number words as a decimal number", 
    "userId": "9yqTaFeQSqvKmNn8B", 
    "author": "Sacha Greif", 
    "submitted": "2017-01-05T03:26:18.908Z", 
    "commentsCount": 4, 
    "comments": [ 
{ 
    "body": "Hello", 
    "postId": "Ad9RYqWqbsJKZx3h7", 
    "userId": "73qGvsRuqNtXcaZDx", 
    "author": "student", 
    "submitted": "2017-01-05T10:26:45.745Z" 
}, 
{ 
    "body": "How are you?", 
    "postId": "Ad9RYqWqbsJKZx3h7", 
    "userId": "73qGvsRuqNtXcaZDx", 
    "author": "student", 
    "submitted": "2017-01-05T10:28:17.225Z" 
} 
    ]} 

カーソルを返すようですが、私はすることはできませんよその上でtoArray()関数を使用してください。

私はこれが(Filtering embedded documents in MongoDB)ことはできませんが、これは6年前だった...

読んでいる私も$sliceaggregateについての記事を見てきたが、それらの周りに私の頭を取得するように見えることはできません。

ご迷惑をおかけして申し訳ありません。

+0

データを購読していますか?単に 'PostsfindOne(postId)'を使い、受け取ったオブジェクトからすべてのコメントを抽出するのはなぜですか? – MasterAM

+0

データが公開されています: 'Meteor.publish( 'comments'、function(postId){ check(postId、String); return Posts.find({postId:postId}); }); router.jsでのサブスクリプションRouter.route( '/ posts /:_id'、{ 名前: 'postPage'、 waitOn:function(){ return [ Meteorスクライブ( 'singlePost'、this.params._id )、 Meteor.subscribe( 'コメント'、this.params._id) ]; }は、 データ:;)}} 、関数(){Posts.findOne(this.params._id)を返す方法 ' – sutebu

+0

受け取ったオブジェクトからコメントを抽出するつもりですか?何かのように: 'var post = Posts.findOne({postId:this._id}); \t var commentList = post.comments.toArray();コメントを返すリスト; '? ありがとうございます。 – sutebu

答えて

0

query this-> Posts.find({"comments.postId":this._id});

0

findOneを使用してコレクションから特定のドキュメントを取得するだけで、forEachループを使用して、そのドキュメントのコメント配列を反復処理することができます。

var post = Posts.findOne({_id: "<id_of_the_post>"}); 

var post_comments = post.comments; // post_comments contains the comments array 

か、各コメントの使用のforEach

post_comments.forEach(function(entry, index, arr){ 
    // entry has the comment object 
//do something with the comment 
    }); 

で何かをしたい場合、私はこのことができます願っています。

関連する問題