Ashesh Khatri
Ashesh Khatri

Reputation: 162

Query on array of array object in mongodb

Following is my product collection in mongodb..

{
    "_id" : ObjectId("56d42389db70393cd9f47c22"),
    "sku" : "KNITCHURI-BLACK",
    "options" : [
        {
            "sku" : "KNITCHURI-BLACK-M",
            "stores" : [
                {
                    "code" : "6",
                    "quantity" : 0
                },
                {
                    "code" : "10",
                    "quantity" : 26
                }
            ],
            "ean_code" : "2709502",
            "size" : "M"
        },
        {
            "sku" : "KNITCHURI-BLACK-S"
            "stores" : [
                {
                    "code" : "6",
                    "quantity" : 0
                },
                {
                    "code" : "10",
                    "quantity" : 30
                }
            ],
            "size" : "S"
        }
    ]
}

want query on where { 'options.stores.code' : '6' } and { 'options.stores.quantity' : { $gt : 0 } }, How i query on this collection then i got response? Kindly help on this issue..

Upvotes: 0

Views: 44

Answers (3)

notionquest
notionquest

Reputation: 39166

Here is the two approaches to get documents in Mongoose.

You can change the Mongo query if required as given in the various answers. I have just used your approach for Mongo query.

Approach 1 - Using cursor - Recommended Approach
Approach 2 - Using Query

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;

var Store = new Schema({
    code : String,
    quantity : Number
});

var Option = new Schema({
    sku : String,
    stores : [ Store ],
    ean_code : String,
    size : String

});

var productSchema = new Schema({
    _id : ObjectId,
    sku : String,
    options : [ Option ]
});

var Product = mongoose.model('product', productSchema, 'product');

console.log("Cursor Approach #1");
var cursor = Product.find({
    'options.stores' : {
        $elemMatch : {
            code : '6',
            quantity : {
                $gt : 0
            }
        }
    }
}).cursor();

console.log("Curosr Results***************************************");
cursor.on('data', function(doc) {
    console.log("Getting doc using cursor ==========>" + JSON.stringify(doc));
});
cursor.on('close', function() {
    console.log("close the cursor");
});

console.log("Query Approach #2");

var query = Product.find({
    'options.stores' : {
        $elemMatch : {
            code : '6',
            quantity : {
                $gt : 0
            }
        }
    }
});

console.log("Query Results***************************************");
query.exec(function(err, doc) {
    if (err) {
        console.log(err);
    }

    console.log("Getting doc using query ==========>" + JSON.stringify(doc));
});

Upvotes: 1

Andriy Simonov
Andriy Simonov

Reputation: 1288

As far as I understand the question, you want to find a products that are in stock in stores with code 6. The following query does that:

db.collection.find({"options.stores" : {$elemMatch: {"code" : "6", "quantity" : {$gt : 0}}}});

Upvotes: 1

user3782299
user3782299

Reputation: 393

I think you need to unwind options

db.product.aggregate( [ 

{ $unwind: "$options" }
{ $match: { 'options.stores.code' : '6','options.stores.quantity' : { $gt : 0 }}


] )

Upvotes: 1

Related Questions