sil*_*ter 11 javascript mongoose mongodb node.js
我如何查询大于或小于特定日期的评论...
这是我的Schema with 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);
});
Run Code Online (Sandbox Code Playgroud)
我需要帮助查询嵌入式文档...
好我编辑了我的代码以提供更多细节.
这将返回一个空数组.所以我想要的或者我期望的是一个带有空注释数组的帖子.但是通过这个查询,我根本没有收到任何帖子.(当我用$ lte兑换$ gte时,我得到(显然)有2条评论的帖子).
但是,我如何根据上面的描述过滤细节呢?
Joh*_*yHK 19
使用点表示法到达嵌入式数组文档内部.例如,要查询以及之间的Post注释和:created_atdate1date2
Post.find({ "comments.created_at": { $gt: date1, $lt: date2 }}, function (err, docs) {
...
});
Run Code Online (Sandbox Code Playgroud)
UPDATE
谢谢你的编辑; 现在我知道你试图按照created_at日期过滤单个帖子的评论.你不能直接使用MongoDB查询,但我相信你可以使用2.2聚合框架,如果你在那个版本.有关示例,请查看Jira 上此功能请求的讨论.
| 归档时间: |
|
| 查看次数: |
21875 次 |
| 最近记录: |