AcidBurn
AcidBurn

Reputation: 103

Javascript - Model find method for Mongodb Collection not work with filter conditions

I'm developing a project with Loopback framework and Mongodb. And I'm facing a problem about find document in Collection of Mongodb from application layer. It's not working with filter condition. I always get all document in Collection.

Here is my model schema

    {
  "name": "MyModel",
  "plural": "mymodel",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "string",
      "required": true
    },
    "guide_to_make": {
      "type": "string",
      "required": true
    },
    "media": {
      "type": "object",
      "required": true
    },
    "price": {
      "type": "number",
      "required": true,
      "default": 0
    },
    "discount": {
      "type": "number",
      "default": 0
    },
    "status": {
      "type": "number",
      "required": true,
      "default": 1
    },
    "user_provider": {
      "type": "string",
      "required": true
    },
    "create_at": {
      "type": "date",
      "required": true,
      "defaultFn": "now"
    },
    "last_update": {
      "type": "date",
      "required": true,
      "defaultFn": "now"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [
    {
      "accessType": "WRITE",
      "principalType": "ROLE",
      "principalId": "$unauthenticated",
      "permission": "DENY"
    }
  ],
  "methods": {}
}

And here is some way which I used to query with filter to find items which match with filter conditions:

MyModel.find({user_provide:'5ae9e8400e59ae0ecc5ed614'},(err,items)=>{console.log(JSON.stringify(items))});

MyModel.find({user_provide:{'like':'5ae9e8400e59ae0ecc5ed614'}},(err,items)=>{console.log(JSON.stringify(items))});

Or I tried to directly used mongo connection as below :

var MongoClient = require('mongodb').MongoClient;
    var url = "mongodb://localhost:27017/";

    MongoClient.connect(url, function (err, db) {
        if (err) throw err;
        var dbo = db.db("asiodb");
        dbo.collection("Mymodel").find({}, { user_provider: '5ae9e8400e59ae0ecc5ed614' }).toArray(function (err, result) {
            if (err) throw err;
            console.log(result);
            db.close();
        });
    });

But with all ways which I used, I always got all documents in Collection. And I've tested with filter conditions in Robo3T with mongo query command

db.getCollection('Mymodel').find({user_provider:'5ae9e8400e59ae0ecc5ed614'})

And it's work as well with mongo command in Robo3T. It can find items which match with conditions I don't know why it's not work on my server layer with JS code.

Someone kindly show to me what is wrong in JS code or the right syntax to use mongo find method.

Many thanks !

Upvotes: 0

Views: 447

Answers (1)

tavarest
tavarest

Reputation: 11

UPDATED:

Using the node mongodb driver you should use your filter in the first parameter of the find function and not in the second, as you did in your code.

It should be something like this:

var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var url = "mongodb://localhost:27017/asiodb";

MongoClient.connect(url, function(err, db) {
    if (err) throw err;

    db.collection('Mymodel').find({ user_provider : '5ae9e8400e59ae0ecc5ed614' }, {}).toArray(function(err, result) {
        if (err) throw err;
        console.log(result);
        db.close();
    });
});

Upvotes: 1

Related Questions