简体   繁体   中英

find object from mongodb

In the data bellow, I would like to find the reminder where _id=abc1 and the month is 1 . The date stored in db is text.

I try to use this command but it have error:
db.check.find( {_id:"abc1"}, { reminder: { $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" }

{
_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 :

  1. The first one is to aggregate your search in another to get only the month.
  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

However it will return the full object and not a partial object as apparently you want to.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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