4

I have array of object field in loopback model.Want to use "inq" option to filter by day.Already have seen docs but those are for array of strings,not the one Iam finding.

weekDays": [
        {
          "day": "Monday",
          "startTime": "03:45",
          "endTime": "04:23"
        },
        {
          "day": "Wednesday",
          "startTime": "03:23",
          "endTime": "12:23"
        }

Syntax for array of string is like {weekDays:{inq:[]}} ,help what modification has to be done here.

3
  • have you tried where : { day: { inq: ['Monday'] } } on your weekDays model ? Commented Sep 20, 2018 at 2:54
  • @Daniel Thanks for your response.WeekDays is not model,its one of the property of my model. Commented Sep 20, 2018 at 4:21
  • can you try like {'weekDays.day' : {$in: ["Monday"]}} as its work in robo 3T like db.getCollection('user').find({'weekDays.day' : {$in: ["Monday"]}}) Commented Sep 20, 2018 at 7:30

1 Answer 1

0

You can use two way in MongoDB

1.Simple find method

db.getCollection('user').find({'weekDays.day' : {$in: ["Monday"]}})

2.By using aggregate

db.getCollection('user').aggregate([
        {$unwind:'$weekDays'},
        {$match : {'weekDays.day' : {$in : ['Monday']}}},
         { "$group": {
        "_id": "$id",
         "weekDays" : { "$push": "$weekDays" },
        }},
    ])

3.aggregate in loopback

var collection = ModelName.getDataSource().connector.collection("myCollection");

collection.aggregate(
  [ 
    { $unwind:'$weekDays' },
    { $match : {'weekDays.day' : {$in : ['Monday']}}},
    { "$group": { "_id": "$id", "weekDays" : { "$push": "$weekDays" }}},
  ],
  function(err, data) {
    if (err) {
    } else {
      console.lod(data)
      });
    }
  }
);
Sign up to request clarification or add additional context in comments.

1 Comment

I am using loopback-connector-composer for database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.