mongodb - 命令失败,错误 16020 (Location16020) : 'Expression $eq takes exactly 2 arguments. 1 were passed in.' on server localhost:27017

标签 mongodb

enter image description here 我的代码如下

以下是我收到的错误:

Command failed with error 16020 (Location16020): 'Expression $eq takes exactly 2 arguments. 1 were passed in.' on server localhost:27017. The full response is {"ok": 0.0, "errmsg": "Expression $eq takes exactly 2 arguments. 1 were passed in.", "code": 16020, "codeName": "Location16020"}

代码

db.chats.aggregate([
    {
        $match: {
            users: {$in: [ObjectId("5e063bcf3e484f6430ec9053")]}
        }
    },

    {

        $lookup:{
            from: "users",
            localField : "users",
            foreignField : "_id",
            as: "users"
        }
    },
    { $lookup: {
            from: "chats.messages",
            as: "last_message",
            let: { id: '$_id' },
            pipeline: [
                { $match: {
                        $expr: { $eq: [ '$$id', '$chat_id' ] }
                    }},
                {$sort: { _id: -1 } },
                { $limit: 1 }
            ]
        }
    },
    ///////Fields
    {
        $project: {
            _id: 1,
            "title": 1,
            "type":1,
            "users._id":1,
            "users.first_name":1,
            "users.last_name":1

        }
    }

])

最佳答案

我在 Intellij 2019.3 中执行查询时遇到了同样的问题。 Intellij使用的mongo jdbc驱动程序版本是v1.7.1。我将其切换为使用 v1.6,然后查询正常执行。

您可以在“数据源和驱动程序”窗口中更改驱动程序版本,方法是单击“MongoDB”驱动程序,然后在“驱动程序文件”部分中单击“版本 1.7.1”,选择“1.6” ”,然后从下拉菜单中单击“应用”。您需要断开连接,然后重新连接,以便它使用新的驱动程序。

enter image description here

关于mongodb - 命令失败,错误 16020 (Location16020) : 'Expression $eq takes exactly 2 arguments. 1 were passed in.' on server localhost:27017,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59511861/

相关文章:

javascript - Express 中的动态路由, "Cannot read property ' concat' of undefined”错误

mongodb - golang mongodb (mgo) 没有插入文档

php - MongoDB 中的 mysql BETWEEN 等价物

java - 使用 Hibernate 定义两种数据源 - 一种是关系型数据源,一种是 NoSQL 数据源

javascript - 未处理的 promise 拒绝警告 : TypeError: Cannot read property 'title' of undefined

java - Spring Boot MongoRepository 忽略验证

java - 以波兰表示法的简单形式合并两个数组列表

javascript - 等到来自 mongoDB 查询的所有项目都被处理后再继续

javascript - 如何将参数传递给 Mongo 脚本

mongodb - 查找字段不存在的所有文档,加上如果字段存在则应用条件