简体   繁体   English

从mongodb查找对象

[英]find object from mongodb

In the data bellow, I would like to find the reminder where _id=abc1 and the month is 1 . 在下面的数据中,我想找到_id=abc1且月份为1的提醒。 The date stored in db is text. db中存储的日期是文本。

I try to use this command but it have error: 我尝试使用此命令,但有错误:
db.check.find( {_id:"abc1"}, { reminder: { $regex: {date:2015-1/} }} ).pretty(); db.check.find({_id:“ abc1”},{提醒:{$ regex:{date:2015-1 /}}}).pretty();

How can I do it? 我该怎么做?
The expected result is { date: "2005-1-5", event: "MeetingB" }, { date: "2005-1-4", event: "MeetingA" } 预期的结果是{日期:“ 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" } ]
}

It think you have 2 solutions : 它认为您有2个解决方案:

  1. The first one is to aggregate your search in another to get only the month. 第一个是将搜索aggregate到另一个中,仅获得月份。
  2. Query on the date 查询日期

With this example (I haven't tested but it should looks like this): 在这个例子中(我还没有测试过,但是看起来应该像这样):

db.check.find( { 
  $and: [ 
    { "_id": { $in: ["abc1"] } }, 
    { "reminder.date": { $in: [/2005-1*/] }  } 
  ] 
} );

You cannot use regex in a in and you have to use JavaScript regex 您不能在in使用regex ,而必须使用JavaScript regex

However it will return the full object and not a partial object as apparently you want to. 但是,它将返回完整的对象,而不是显然要返回的部分对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM