简体   繁体   中英

Filter by field and select another field mongodb

I have used mongoose with my application and trying to retrieve the documents which has type as country and taking the metadata field of it.

my document looks as follows,

在此处输入图片说明

i have in code,

  dbHelper.query(mongoose.model('_entity'), {
                'type': 'country'
            }, function (error, data) {                
                callback(data);
            });

dbHelper:

query: function (model, conditon, options) {
            return new Promise(function (resolve, reject) {
                options = options || {};
                model.find(conditon, {}, options, function (error, data) {
                    if (error)
                        console.log("error is"+err);
                        reject(error);
                    console.log("data is "+data);
                    resolve(data);
                })
            })
        }

how this should be changed in order to select the metadata field?

You need select :

mongoose
  .model('_entity')
  .find()
  .where('type').equals('country')
  .select('metadata')
  .exec( function(error, data) {
      callback(data);
  });

Update: for example projection = ["metadata", "name"]

query: function (model, conditon, projection, options) {
            return new Promise(function (resolve, reject) {
                options = options || {};
                projection = projection || [];
                model.find(conditon, projection.join(" "), options, function (error, data) {
                    if (error)
                        console.log("error is"+err);
                        reject(error);
                    console.log("data is "+data);
                    resolve(data);
                })
            })
        }

You can change your find query a bit to select only the metada or whichever fields you want.

model.find(conditon, {"metadata" : 1}, options, function (error, data) {
    if (error)
        console.log("error is"+err);
        reject(error);
    console.log("data is "+data);
    resolve(data);
});

If you can't add metadata in the existing find query , you can try alternate method.

add another argument projection to the query function.

query: function (model, conditon, projection, options) {
    return new Promise(function (resolve, reject) {
        options = options || {};
        projection = projection || {};
        model.find(conditon, projection, options, function (error, data) {
            if (error)
                console.log("error is"+err);
                reject(error);
            console.log("data is "+data);
            resolve(data);
        })
    })
}

And use it like this:

dbHelper.query(mongoose.model('_entity'), {
    'type': 'country'
},{
    'metadata'  : 1//add other required fields if needed
}, function (error, data) {                
    callback(data);
});

I think this will work in your case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM