0

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..

3 Answers 3

1

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));
});
Sign up to request clarification or add additional context in comments.

Comments

1

I think you need to unwind options

db.product.aggregate( [ 

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


] )

Comments

1

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}}}});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.