【发布时间】:2014-04-17 05:08:52
【问题描述】:
我正在尝试查找特定记录(ID 为1),然后仅返回其history 字段。这个history 字段是一个具有timestamp 属性的对象数组。当我返回这个历史字段时,我希望它只包含 timestamp 属性大于 0 的对象。
样本收集
[{
"_id" : 1,
"history" : [
{
"content" : "hello",
"timestamp" : 1394639953878,
"_id" : ObjectId("53208451767743b748ddbd7d")
},
{
"content" : "world",
"timestamp" : 1394639953879,
"_id" : ObjectId("33208451767743b748ddbd7e")
}
]
}]
因此,根据 Mongo 和 Mongoose 文档,我会执行以下操作:
model.find({_id: 1})
.select({ history: { $elemMatch: { timestamp: { $gt: 0 } } } });
问题: 我知道只有一个结果(因为我选择了 ID 1),但我使用 $elemMatch 用 填充其 history 字段时间戳大于0 的所有 对象。问题是history 中只有一个对象。
【问题讨论】:
-
“时间戳”是干什么用的?
-
timestamp只是history字段中包含的对象的任意数字属性。 -
你能发一份样本文件吗?
-
添加了样本集。
-
你具体选择一个文档的by_id字段知道吗??
标签: javascript node.js mongodb mongoose database