【发布时间】:2016-06-17 10:36:18
【问题描述】:
我有一个像这样的猫鼬模型:
var activityItem = mongoose.Schema({
timestampValue: Number,
xabc: String,
full: Boolean,
comp: Boolean
});
var ABC = mongoose.Schema({
activity: [activityItem],
user: {
type: mongoose.Schema.ObjectId,
ref: 'User'
},
username: String
});
我想获取 timestampValue 小于特定值的 activityItem 数组元素。另外,我想先按照timestampValue对activity数组进行排序
这是我目前拥有的代码。而且它不起作用。
UserActivity.findOne({
'user': current_user,
'activity' : {
$all: [
{
"$elemMatch": {
timestampValue: {
$lte: time
}
}
}
]
}
},
function(err, user){
})
示例文档结构:
{
"_id" : ObjectId("56d5e88adfd14baf1848a7c6"),
"user" : ObjectId("56bf225342e662f4277ded73"),
"notifications" : [],
"completed" : [],
"activity" : [
{
"timestampValue": 1456902600000,
"xabc": "Some value",
"full": true,
"comp": false,
"_id" : ObjectId("56d5e88adfd14baf1848a7d2")
},
{
"timestampValue": 1456702600000,
"xabc": "Some other value",
"full": true,
"comp": false,
"_id" : ObjectId("56d5e88adfd14baf1848a7d3")
}
],
"__v" : 1
}
POST 调用具有以下参数
hash: "2e74aaaf42aa5ea733be963cb61fc5ff"
time: 1457202600000
hash 出现在我从 mongo 获得文档后
time 是一个 unix 时间戳值。
它不是只返回小于time 值的元素,而是返回所有数组元素。我在查询之前尝试了聚合框架对数组进行排序,但无法掌握它。
任何帮助将不胜感激。
【问题讨论】:
-
添加到帖子中的示例输入文档。
标签: node.js mongodb mongoose aggregation-framework