user9600581
user9600581

Reputation: 31

how to query for distinct date result in mongodb?

This is my document schema.

   const smartpeepeeSchema = new Schema({
        owner: {
            type: mongoose.Types.ObjectId,
            required: true,
            ref: 'User',
        },
        baby: {
            type: mongoose.Types.ObjectId,
            required: true,
            ref: 'Baby'
        },
        data: [
            {
                _id: {
                    autoIndex: false
                },
                serialNumber: {
                    type: String,
                },
                macAddress: {
                    type: String,
                },
                firmware: {
                    type: String,
                },
                createdAt: {
                    type: Date,
                },
                humidity: {
                    type: Number,
                },
                temperature:{
                  type:Number
                },
                type:{
                    type:String
                }
            }
        ]
    });

and these are stored date as createdAt in my mongodb.

{ _id: 5c73e430ab6f3242b5c141c3,
  owner: 5c73c2e039ecd63e0edfebf1,
  baby: 5c73c2ea39ecd63e0edfebf2,
  data:[ 
      {createdAt: 2019-02-25T12:48:48.499Z,
       temperature: 23.2891,
       humidity: 1.82187,
       serialNumber: '01202117082100191',
       macAddress: 'D2:6A:95:5E:48:6E',
       firmware: '233',
       type: 'diaper' },
     { createdAt: 2019-02-25T12:48:48.499Z,
       temperature: 62.2587,
       humidity: 19.0473,
       serialNumber: '01202117082100191',
       macAddress: 'D2:6A:95:5E:48:6E',
       firmware: '233',
       type: 'diaper' },
     { createdAt: 2019-02-25T12:48:48.499Z,
       temperature: 19.8427,
       humidity: 68.2203,
       serialNumber: '01202117082100191',
       macAddress: 'D2:6A:95:5E:48:6E',
       firmware: '233',
       type: 'diaper' },
     { createdAt: 2019-02-25T12:48:48.499Z,
       temperature: 55.5166,
       humidity: 14.4124,
       serialNumber: '01202117082100191',
       macAddress: 'D2:6A:95:5E:48:6E',
       firmware: '233',
       type: 'diaper' } 
],
  __v: 0 }

I want the result like this ?

 {createdAt: specific date,
           temperature: 23.2891,
           humidity: 1.82187,
           serialNumber: '01202117082100191',
           macAddress: 'D2:6A:95:5E:48:6E',
           firmware: '233',
           type: 'diaper' },
         { createdAt: specific date,
           temperature: 62.2587,
           humidity: 19.0473,
           serialNumber: '01202117082100191',
           macAddress: 'D2:6A:95:5E:48:6E',
           firmware: '233',
           type: 'diaper' },
         { createdAt: specific date,
           temperature: 19.8427,
           humidity: 68.2203,
           serialNumber: '01202117082100191',
           macAddress: 'D2:6A:95:5E:48:6E',
           firmware: '233',
           type: 'diaper' },

**> I want to retrieve data for a specific date.

if the client send to backend a data like '2019-02-24.

The client must fetch all data for that date

What should I do?..

thank you for reading above all.

from foreign developer.**

Upvotes: 0

Views: 314

Answers (1)

Arsen Davtyan
Arsen Davtyan

Reputation: 1957

Try using $gte and $lte in your query, by adding time portion on your date. So the following range will represent the day:

00:00:00:000 -- 23:59:59.999

In your case if the input date is 2019-02-24:

db.collection.find({"DateField" : {$gte : ISODate("2019-02-24T00:00:00.000Z"), "$lte" : ISODate("2019-02-24T23:59:59.999Z")}})

This query will return all the document which have a DateField for 2019-02-24.

EDIT

In order to achieve what you want, you can run aggregate:

db.collection.aggregate(
[
    {$unwind : "$data"}, // This will open up your array
    {$match : {"data.createdAt" : {$gte : ISODate("2019-02-24T00:00:00.000Z"), "$lte" : ISODate("2019-02-24T23:59:59.999Z")}}} // This will filter to get only the date you need
]);

This will return only the object from the array, when createdAt : 2019-02-24:

Upvotes: 2

Related Questions