[英]find object from mongodb
在下面的数据中,我想找到_id=abc1
且月份为1
的提醒。 db中存储的日期是文本。
我尝试使用此命令,但有错误:
db.check.find({_id:“ abc1”},{提醒:{$ regex:{date:2015-1 /}}}).pretty();
我该怎么做?
预期的结果是{日期:“ 2005-1-5”,事件:“ MeetingB”},{日期:“ 2005-1-4”,事件:“ MeetingA”}
{
_id: "abc1", reminder:[ { date: "2005-1-5", event: "MeetingB" }, { date: "2005-1-4", event: "MeetingA" }, { date: "2005-2-4", event: "MeetingA" } ] }
{
_id: "abc2", reminder:[ { date: "2005-1-5", event: "MeetingB" } ]
}
它认为您有2个解决方案:
aggregate
到另一个中,仅获得月份。 在这个例子中(我还没有测试过,但是看起来应该像这样):
db.check.find( {
$and: [
{ "_id": { $in: ["abc1"] } },
{ "reminder.date": { $in: [/2005-1*/] } }
]
} );
您不能在in
使用regex
,而必须使用JavaScript regex
但是,它将返回完整的对象,而不是显然要返回的部分对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.