简体   繁体   中英

MongoDB query get count between two dates

I have 10000's of data in my monogodb table, need write a query to get the count between two dates. below is the response looks like from mongodb table

{ _id: 5942c8e366ae6b3aefb37232,
    serialnumber: 5523330044,
    model: ‘MODEL1’,
    version: 'EM.12.12.12',
    releasedata: '2017-06-01',
    modelid: 90000001,
    uiversion: 'EM.12.12.12',
    imageversion: 'EM.M1.M1.M1',
    ecnnumber: null },
  { _id: 5942c8e366ae6b3aefb37231,
    serialnumber: 5523330043,
    model: 'MODEL1',
    version: 'EM.12.12.12',
    releasedata: '2017-06-01',
    modelid: 90000001,
    uiversion: 'EM.12.12.12',
    imageversion: 'EM.M1.M1.M1',
    ecnnumber: null },
  { _id: 5942c8e366ae6b3aefb37233,
    serialnumber: 5523330045,
    model: 'MODEL1',
    version: 'EM.12.12.12',
    releasedata: '2017-06-01',
    modelid: 90000001,
    uiversion: 'EM.12.12.12',
    imageversion: 'EM.M1.M1.M1',
    ecnnumber: null },
  { _id: 5942c8e366ae6b3aefb37234,
    serialnumber: 5523330046,
    model: 'MODEL1',
    version: 'EM.12.12.12',
    releasedata: '2017-06-01',
    modelid: 90000001,
    uiversion: 'EM.12.12.12',
    imageversion: 'EM.M1.M1.M1',
    ecnnumber: null },
  { _id: 5942c8e366ae6b3aefb37235,
    serialnumber: 5523330047,
    model: 'MODEL1',
    version: 'EM.12.12.12',
    releasedata: '2017-06-01',
    modelid: 90000001,
    uiversion: 'EM.12.12.12',
    imageversion: 'EM.M1.M1.M1',
    ecnnumber: null },
  { _id: 5942c8e366ae6b3aefb37237,
    serialnumber: 5523330049,
    model: 'MODEL1',
    version: 'EM.12.12.12',
    releasedata: '2017-06-01',
    modelid: 90000001,
    uiversion: 'EM.12.12.12',
    imageversion: 'EM.M1.M1.M1',
    ecnnumber: null },

What i have tried, form the below code i got the count of "1000", but i actually need to write a query which give the count of registered devices form start and end dates.

In the above response i have the releasedate key to query between dates.

please help me in finding the solution.

app.post('/getTheCount', function (req, res) {

    var collection    =    gdb.collection('machine');
    var modelid       =    parseInt(req.body.modelid);
    collection.count({modelid:modelid}, function(error, numOfDocs) {
     console.log('I have '+numOfDocs+' documents in my collection');
     res.send({numOfDocs});

     });

 })

Thanks,

i would try this with $gte and $lte in mongodb.

db.machine.find({
    releasedata: {
        $gte: ISODate("2010-04-29T00:00:00.000Z"),
        $lt: ISODate("2010-05-01T00:00:00.000Z")
    }
}).count();

If releasedata iy not your date to search the change it. I would suggest to you to store date not as string. Try it like this:

> db.test.insert({date: ISODate()})
> db.test.insert({date: new Date()})
> db.test.find()
{ "_id" : ObjectId("..."), "date" : ISODate("2010-04-29T00:00:00.000Z") }
{ "_id" : ObjectId("..."), "date" : ISODate("2010-05-01T00:00:00.000Z") }

If you want for single collection in DB you can use

db.collection_name.find({
  "createdAt": {
    $gte: ISODate("2022-11-29T00:00:00.000Z"),
    $lte: ISODate("2022-11-30T00:00:00.000Z")
  }
}).count();

If you want to get count of all collections present in DB

db.getCollectionNames().forEach(function(collection) {
  count = db[collection].find({
    "createdAt": {
      $gte: ISODate("2022-11-29T00:00:00.000Z"),
      $lte: ISODate("2022-11-30T00:00:00.000Z")
    }
  }).count();print( collection + " count is : " + count);
});

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