looking for some solutions? You are welcome.

SOLVED: How to update a document based on two filter conditions?

That Guy:

I have a document structure that looks like this:

type Document = {
  _id: string
  title: string
  variants: VariantType[]
}

type VariantType = {
  timestamp: Int
  active: Boolean
  content: any[]
}

I'm trying to filter a document based on two filter conditions in one query. First I want to match the _id and then find a specific variant based on a timestamp.

My previous version of the query was filtering based on the active key.

const updatedDocument = await allDocuments
        .findOneAndUpdate({ _id: mongoId, 'variants.active': false }, .... };

Changing it to

const updatedDocument = await allDocuments
        .findOneAndUpdate({ _id: mongoId, 'variants.timestamp': timestamp }, .... };

returns null.

Can mongo even match a document like this. I saw that there is an $eq query selector but I can't seem to get it working either.



Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
Share:

No comments:

Recent