MongoDB: find by dates nested in an object field, without using $ or

My sample document:

{
    name  : 'ABC123',
    active: false,
    meta  : {
        createdBy: 'user@gmail.com',
        created  : ISODate("2017-11-20T13:04:15.757Z"), // 13:04
        editedBy : 'user2@gmail.com',
        edited   : ISODate("2017-11-20T13:06:06.033Z"), // 13:06
        removedBy: 'user@gmail.com',
        removed  : ISODate("2017-11-20T13:09:11.033Z")  // 13:09
    }
}

How to fulfill a request for all documents that have:

  • meta.created
  • meta.updated
  • meta.removed

field $gtthan the date I specify ( without$or )?

What I have now:

const d = ISODate("2017-11-20T13:00:00.033Z"); // 13:00
db
.collection('example')
.find(
    { 
        $or: ['meta.removed', 'meta.edited', 'meta.created'].map(m => { 
            return { [m]: { $gt: d } } 
        })
    } 
);

What I tried and did not work:

const d = ISODate("2017-11-20T13:09:11.033Z"); // 13:00
db
.collection('example')
.find(
    { 
        'meta.$': { $gt: d }
    } 
);
+4
source share

Source: https://habr.com/ru/post/1689570/


All Articles