【发布时间】:2019-06-28 05:46:12
【问题描述】:
我有一个在 MongoDB Compass 实用程序中运行时运行良好的查询:
{ $where: "if (this.media && this.media.length > 1 && this.status=='Published') {return this; } "}
在同一个集合或表中,我还有两个字段 createBy 和 userId
现在我想过滤 createdBy 与 userId 不同的记录。我试试这个:
{ $where: "if (this.media && this.media.length > 1 && this.status=='Published' && this.userId != this.createdBy) {return this; } "}
还有这个:
{ $where: "if (this.media && this.media.length > 1 && this.status=='Published' && ObjectId(this.userId) != ObjectId(this.createdBy)) {return this; } "}
但是以上两个都不起作用。我知道 ObjectId 是 Object 类型,并且比较具有完全相同值的对象是行不通的。只是不知道如何在 mongodb 查询控制台中进行比较。
【问题讨论】:
-
你用的是什么版本的mongodb?
-
@Andyk。我正在使用 v4.0.8。 MongoDB Compass 社区版本是 1.17.0。
标签: mongodb mongodb-query mongodb-compass