特定の日付よりも大きいまたは小さいコメントを照会するにはどうすればよいですか...
Post モデルと Comment モデルを使用した私のスキーマは次のとおりです。
var mongoose = require('mongoose');
var should = require('should');
mongoose.connect("localhost","test_db");
var CommentSchema = new mongoose.Schema({
content: {type:String},
created_at: {type:Date, default:Date.now}
});
var PostSchema = new mongoose.Schema({
title: {type:String},
content: {type:String},
comments: [CommentSchema]
});
var Post = mongoose.model('Post',PostSchema);
var Comment = mongoose.model('Comment',CommentSchema);
var post = new Post({title:"hello world",comments:[{content:"1st comment"},{content:"2nd comment"}]});
// I saved it! and then I query for it
var id = "5045d5be48af9f040f000002";
Post.find({ _id:id,
"comments.created_at":{ $gte:(new Date())}
},function(err,result){
console.log(result);
});
埋め込まれたドキュメントのクエリについて助けが必要です...
詳細を示すためにコードを編集しました。
これは空の配列を返します。したがって、私が望む、または期待するのは、空のコメント配列を持つ Post です。しかし、このクエリでは投稿がまったく得られません。($gte を $lte と交換すると、(明らかに) 2 つのコメントを含む投稿が表示されます)。
しかし、上記の説明に従って、どうすれば詳細をフィルタリングできますか?