简体   繁体   English

如何根据两个过滤条件更新文档?

[英]How to update a document based on two filter conditions?

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. 首先,我想匹配_id,然后根据时间戳找到一个特定的变体。

My previous version of the query was filtering based on the active key. 我以前的查询版本是根据active密钥进行过滤的。

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. 返回null。

Can mongo even match a document like this. mongo甚至可以匹配这样的文档。 I saw that there is an $eq query selector but I can't seem to get it working either. 我看到有一个$eq查询选择器,但我似乎无法让它工作。

Turns out I the problem was with the timestamp I was sending to the server. 事实证明我的问题在于我发送到服务器的时间戳。 I was getting it from the wrong place and it wasn't matching any of the variants. 我从错误的地方得到它,它不匹配任何变种。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM