fmonper1
fmonper1

Reputation: 89

MongoDB Query SubDocuments by field valeu

So I have this schema for a Supplier:

 /**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    Address = require('./Address.js'),
    AddressSchema = mongoose.model('Address').schema,
    Product = require('./Product.js'),
    ProductSchema = mongoose.model('Product').schema;

// Create a new schema for the reviews collection with all the relevant information for a review
var Schema = mongoose.Schema;

var Supplier = new Schema(
    {
        name: String,
        address: AddressSchema,
        location: {
            type: {type:String, default: 'Point'},
            coordinates: [Number] // [<longitude>, <latitude>]
        },
        products: [ProductSchema]
    }
);

Supplier.index({location: '2dsphere'});

var SupplierModel = mongoose.model('Supplier', Supplier );
// export the review model
module.exports = SupplierModel;

Products in my system have a "verified" field which is a boolean. In one of my routes I would like to query the DB to find all the suppliers which have products which aren't verified such that I can then render those products in the page.

I tried this, but unofrtunatelly it returns all the subdocuments no matter if "verified" is true or false:

exports.admin_accept_product_get = function (req, res) {
    Supplier.find({'products.verified' : false}, function(err, docs) {
        res.render('admin_accept_product', { user : req.user, suppliers: docs });
    });
};

Any help is appreciated

Edit:

The previous query would return the following data:

{
    "_id" : ObjectId("5b2b839a2cf8820e304d7413"),
    "location" : {
        "type" : "Point",
        "coordinates" : [ 
            -16.5122377, 
            28.4028329
        ]
    },
    "name" : "Tienda1",
    "products" : [ 
        {
            "verified" : true,
            "_id" : ObjectId("5b2b83d32cf8820e304d7420"),
            "title" : "Vodka",
            "inStock" : 15,
            "typeOfItem" : "alcohol",
            "sellingPrice" : 15,
            "image" : "public/upload/15295784515201529168557789bottle.png",
            "typeOfAlcohol" : "vodka"
        }, 
        {
            "verified" : false,
            "_id" : ObjectId("5b2b848f8c59960c44df09cd"),
            "title" : "Whisky",
            "inStock" : 40,
            "typeOfItem" : "alcohol",
            "sellingPrice" : 15,
            "image" : "public/upload/15295786395491529323314298whisky.png",
            "typeOfAlcohol" : "whisky"
        }
    ],
    "__v" : 2
}

I would like my query to not return the firt product because "verified == true"

Upvotes: 1

Views: 39

Answers (1)

Ashh
Ashh

Reputation: 46491

You need to use $elemMatch to find the document and $elemMatch for projection of the data

db.collection.find({
  products: {
    $elemMatch: {
      verified: false
    }
  }
},
{
  products: {
    $elemMatch: {
      verified: false
    }
  },
  location: 1
})

Output

[
  {
    "_id": ObjectId("5b2b839a2cf8820e304d7413"),
    "products": [
      {
        "_id": ObjectId("5b2b848f8c59960c44df09cd"),
        "image": "public/upload/15295786395491529323314298whisky.png",
        "inStock": 40,
        "sellingPrice": 15,
        "title": "Whisky",
        "typeOfAlcohol": "whisky",
        "typeOfItem": "alcohol",
        "verified": false
      }
    ]
  }
]

Check it here

Upvotes: 1

Related Questions