Loopback: How to make query for array of object?

Created on 27 Dec 2016  路  12Comments  路  Source: strongloop/loopback

Hello everyone,

I have a requirement to get the data from array of objects,which is seems like below object. i have to make query based on "id" from colors array?.please can anyone suggest the query.

[
{
"id":"123123213",
"name":"Some Name",
"colors":[
{
"colorId":"1"
},
{
"colorId":"2"
}
]
}
]

Most helpful comment

@narender56 Try this : {"where":{"colors":{"elemMatch":{"colorId":"1"}}}}

All 12 comments

Hey @narender56 what kind of query do you want? create or find or something else? Can you elaborate more?

Hey @jannyHou , i just want to get the data from db.Its a find query based on nested array of objects parameters.. colorId is the filter

@narender56 Try this : {"where":{"colors":{"elemMatch":{"colorId":"1"}}}}

Thank you @massilvaLaz , It worked. :-)

[
{
"id":"123123213",
"name":"Some Name",
"colors":[
{
"colorId":"1"
"colorName:"Red"
},
{
"colorId":"2",
"colorName:"Blue"
}
]
}
]

Hello, this thread help me so much.
How can i update the colorName properties from colorId "1" ?
So basicly i want to upgrade the colorName property only in colorId 1 to Green.
please advise me.
Thank you so much.

hey @khanisak, You can try this

db.collection.updateOne(
    {"colors":{"elemMatch":{"colorId":"1"}}},
    { $set: { "colors.$.colorName" : 'Green'} }
)

@narenderv7 it works if I querying inside mongodb, but i doesn't work if i querying from loopback model.

If you want to do something a little more complex, like use a gte or lte, you have to add an $ at the start. For example:

{
  "where": {
    "operations": {
      "elemMatch": {
        "value": { "$gt": 0 }
      }
    }
  }
}

{
"activities": [
{
"visitTime": "2019-01-23T12:20:11.923Z",
"lat": "9.4634371",
"lng": "77.7949438"
},
{
"visitTime": "2019-01-23T12:20:45.601Z",
"lat": "9.4634371",
"lng": "77.7949438"
},
{
"visitTime": "2019-01-23T12:21:27.904Z",
"lat": "9.4634371",
"lng": "77.7949438"
}
]
}

My Data like this how to get it visitTime by passing query with {"between": ["'+ year +'-'+ month+'-'+ date +' 00:00:00", "'+ year +'-'+ month +'-'+ date +' 23:59:59"]} . I have problem with it please help me

@narender56 Try this : {"where":{"colors":{"elemMatch":{"colorId":"1"}}}}

it's not helped for me. I get error

unknown operator: $visitTime

@narender56 Try this : {"where":{"colors":{"elemMatch":{"colorId":"1"}}}}
This might have work for mongodb but not working with mysql.

Solution {"where":{"colors":{"elemMatch":{"colorId":"1"}}}} is not working with date field.

I have the below data in MongoDB collection. I want to find the examPaperId detail from the timetable according to the current date.
{
"status": "Active",
"createdOn": "2021-02-14T17:01:19.008+05:30",
"updatedOn": "2021-02-14T17:01:19.008+05:30",
"deleted": false,
"timeTable": [{
"examPaperId": "6029094feb756b16ccccfa31",
"examPaperSetId": "",
"startDate": "2021-02-12T10:00:00.528Z",
"endDate": "2021-02-12T12:30:00.528Z",
},{
"examPaperId": "6029094feb756b16ccccfa32",
"examPaperSetId": "",
"startDate": "2021-02-13T10:00:00.528Z",
"endDate": "2021-02-13T12:30:00.528Z",
},{
"examPaperId": "6029094feb756b16ccccfa3",
"examPaperSetId": "",
"startDate": "2021-02-14T10:00:00.528Z",
"endDate": "2021-02-14T12:30:00.528Z",
}]],
"examId": {
"$oid": "6028f2c89a948912f0d8eea9"
},
"className": {
"$oid": "5fff0e51f48b61bbff1c2a02"
}
}

I am using where condition as below:
where: {
and: [
{status: StatusEnum.ACTIVE},
{className: userData.studentAttribute.className},
{
timeTable: {
elemMatch: {
and: [
{
startDate: {
lte: new Date(),
},
},
{
endDate: {
gt: new Date(),
},
},
],
},
},
},
],
},

Any suggestion for make it working?

Was this page helpful?
0 / 5 - 0 ratings