Please help to make query in mongodb. I want mongo query in which sort array objects using some expression. Example. I have documents like
{
"_id:"5e0846c922427bdc824d3943",
"isActive" : true,
"customFieldArray" : [
{
"custom_field_id" : "fname",
"field_value" : "kevin"
},
{
"custom_field_id" : "lname",
"field_value" : "patel"
},
{
"custom_field_id" : "city",
"field_value" : "surat"
}
],
"creationMode" : "adminForm",
"createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
"updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
"gender" : "Boy"
},
{
"_id":"5e08471d6053718c808cbab8",
"isActive" : true,
"customFieldArray" : [
{
"custom_field_id" : "fname",
"field_value" : "alice"
},
{
"custom_field_id" : "lname",
"field_value" : "somani"
},
{
"custom_field_id" : "city",
"field_value" : "bharuch"
}
],
"creationMode" : "adminForm",
"createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
"updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
"gender" : "Boy"
},
{
"_id":"5e0847392b8622f8100267e3",
"isActive" : true,
"customFieldArray" : [
{
"custom_field_id" : "fname",
"field_value" : "Isha"
},
{
"custom_field_id" : "lname",
"field_value" : "Trivedi"
},
{
"custom_field_id" : "city",
"field_value" : "Mumbai"
}
],
"creationMode" : "adminForm",
"createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
"updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
"gender" : "Girl"
}
and when pass filter parameter on custom_field_id = "fname" then it will display results in following ascending or descending way.
{
"_id":"5e08471d6053718c808cbab8",
"isActive" : true,
"customFieldArray" : [
{
"custom_field_id" : "fname",
"field_value" : "alice"
},
{
"custom_field_id" : "lname",
"field_value" : "somani"
},
{
"custom_field_id" : "city",
"field_value" : "bharuch"
}
],
"creationMode" : "adminForm",
"createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
"updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
"gender" : "Boy",
},{
"_id":"5e0847392b8622f8100267e3",
"isActive" : true,
"customFieldArray" : [
{
"custom_field_id" : "fname",
"field_value" : "Isha"
},
{
"custom_field_id" : "lname",
"field_value" : "Trivedi"
},
{
"custom_field_id" : "city",
"field_value" : "Mumbai"
}
],
"creationMode" : "adminForm",
"createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
"updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
"gender" : "Girl",
},
{
"_id:"5e0846c922427bdc824d3943",
"isActive" : true,
"customFieldArray" : [
{
"custom_field_id" : "fname",
"field_value" : "kevin"
},
{
"custom_field_id" : "lname",
"field_value" : "patel"
},
{
"custom_field_id" : "city",
"field_value" : "surat"
}
],
"creationMode" : "adminForm",
"createdAt" : ISODate("2019-11-21T06:40:20.033Z"),
"updatedAt" : ISODate("2019-11-21T06:40:20.033Z"),
"gender" : "Boy"
}
_id
s for clarity on the document structure.