简体   繁体   中英

Find query for MongoDb schema design

I have Kitchen schema which is structured as below, I want to do a find query on this schema to get a package with particular ID and Date from packages array.

{
"_id" : ObjectId("58aacd498caf670a837e7093"),
"name" : "Kitchen 1",
"packages" : [ 
    {
        "package" : ObjectId("58aacd038caf670a837e7091"),
        "availibility" : [ 
            {
                "date" : ISODate("2015-03-25T00:00:00.000Z"),
                "count" : 20
            }, 
            {
                "date" : ISODate("2016-03-25T00:00:00.000Z"),
                "count" : 30
            }
        ]
    }, 
    {
        "package" : ObjectId("58aacd108caf670a837e7092"),
        "availibility" : [ 
            {
                "date" : ISODate("2016-03-25T00:00:00.000Z"),
                "count" : 10
            }
        ]
    }
],
"__v" : 0
}

If I do a find query with package ID(58aacd038caf670a837e7091) and date(2015-03-25T00:00:00.000Z), then response should be like :-

{
 "package" : ObjectId("58aacd038caf670a837e7091"),
 "date" : ISODate("2015-03-25T00:00:00.000Z")
 "count" : 20
}

In mongodb:

You will have to use a loop to see only a single date. For all dates including the matching one for the package id you can do this:

db.collection_name.find({'packages.package':ObjectId("package_id"),'packages.availability.date': ISODate("date")},{'packages.package':1, 'packages.availability':1}).pretty()

In mongoose, I am assuming you have imported the Kitchen schema

Kichen.find({'packages.package':"package_id",'packages.availability.date': "iso_date"}, function(err, package){
   if(err) 
       console.log("There was an error");
   if(package == null){
       console.log("no package found");
   } else {
       //do whatever
   }
});

You can compare values in loop:

db.so.find().forEach(function(po){
  po.packages.forEach(function(co){
    co.availibility.forEach(function(o){
      if(co.package=='58aacd038caf670a837e7091' 
        &&
        String(ISODate("2015-03-25T00:00:00.000Z"))==String(o.date)
      ){
        o.package=co.package;
        printjson(o);
      }
    })
  })
});
{
        "date" : ISODate("2015-03-25T00:00:00Z"),
        "count" : 20,
        "package" : ObjectId("58aacd038caf670a837e7091")
}

You can run an aggregate operation that uses the $filter and $arrayElemAt operators to return the desired fields within a couple of $project pipeline steps.

Consider the following pipeline:

Kitchen.aggregate([
    {
        "$project": {
            "packages": {
                "$arrayElemAt": [
                    {
                        "$filter": {
                            "input": "$packages",
                            "as": "pkg",
                            "cond": {
                                "$eq": [
                                    "$$pkg.package", 
                                    mongoose.Types.ObjectId("58aacd038caf670a837e7091")
                                ]
                            } 
                        }
                    },
                    0
                ]
            }
        }
    },
    {
        "$project": {
            "package": "$packages.package",
            "availibility": {
                "$arrayElemAt": [
                    {
                        "$filter": {
                            "input": "$packages.availibility",
                            "as": "el",
                            "cond": {
                                "$eq": ["$$el.date", new Date("2015-03-25")]
                            } 
                        }
                    },
                    0
                ]
            }
        }
    },
    {
        "$project": {
            "_id": 0,
            "package": 1,
            "date": "$availibility.date",
            "count": "$availibility.count"
        }
    }
]).exec(function(err, docs){
    if (err) throw err;
    console.log(docs);
})

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