Deepak
Deepak

Reputation: 145

Mongodb Query Less than specified time

document as below

chatid:121212,
 messages:[
      {
      msg:'Hello',
      time:'2021-04-17T16:35:25.879Z'
    },
    .
    .
    .
]

I want query all record where time less than specified timestamp.

Upvotes: 1

Views: 312

Answers (1)

Demo - https://mongoplayground.net/p/Kr51g6hb9Qz

Use $unwind

Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.

$match to filter the records

$lt

db.collection.aggregate([
  { $unwind: "$messages" }, //  break into individual documents
  { $match: { "messages.time": { $lt: "2021-04-17T16:39:24.879Z" } } }
])

Upvotes: 2

Related Questions