Search code examples
angularjsnode.jsmongodbloopbackjsangular-loopback

$elemMatch query in looback-angularjs sdk with loopback-mongodb-connector?


I have a collection called Dish in mongodb the sample data is -

[{
"_id": ObjectId("56c839b969431a9913cafa65"),
"name": "Dish 01",
"description": "Dish 01 description",
"ingredients": [{
    "id": "56c4a40bf97c039d44e89185",
    "name": "onion",
}, {
    "id": "56c4a40bf97c039d44e89145",
    "name": "salt",
}, {
    "id": "56c4a40bf97c039d44e89176",
    "name": "spices",
}],
"category": "entree",
"image": "https://s3-ap-southeast-1.amazonaws.com/sichu-bucket/2016-02-20T13:10:31.334Zapi.js"
}, {
"_id": ObjectId("56c839b969431a9913cafa70"),
"name": "Dish 02",
"description": "Dish 02 description",
"ingredients": [{
    "id": "56c4a40bf97c039d44e89185",
    "name": "onion",
}, {
    "id": "56c4a40bf97c039d44e89145",
    "name": "oil",
}],
"category": "main",
"image": "https://s3-ap-southeast-1.amazonaws.com/sichu-bucket/2016-02-20T13:10:31.334Zapi.js"
}]

now I want dishes which has onion in it and I want to get result by using loopback-angularjs-sdk

I have added this to my Dish.json file

"settings": {
    "mongodb": {
      "allowExtendedOperators": true
    }
}

I don't know how it should be done. Please guide me into this.


Solution

  • In your ngApp controller pass the Dish object, and then you can use the Dish.find filter to create a filter api, which will internally search mongo for all the dishes with indegredients as onion.

    Dish.find({"filter": {"where": {"indegredients.id": "56c4a40bf97c039d44e89185"}}}, function (dishes) {
    });
    

    For more on filter api, or how to call them through angular sdk you can use. https://docs.strongloop.com/display/public/LB/Where+filter https://docs.strongloop.com/display/public/LB/AngularJS+JavaScript+SDK

    Plus you can run a local server for the loopback angular sdk documentation also.

    For generating the angular sdk documentation, what you can do is

    lb-ng-doc client/js/services/lb-services.js
    

    Browse the documentation at http://localhost:3030/