【问题标题】:Trying to find record using $in in mongoose试图在猫鼬中使用 $in 查找记录
【发布时间】:2016-10-02 19:01:44
【问题描述】:

我正在尝试在 mongoose 中使用 $in 查找记录。但这对我不起作用。我在 mongo shell 中有相同的查询,但在 mongoose 中它不是 workinh 我的架构

{
      "_id": "574f1f979f44e7531786c80f",
      "name": "mySchool2",
      "branch": "karachi",
      "location": "Clifton ",
      "block": false,
      "created_at": 1464803223441,
      "updated_at": 1464803223441,
      "__v": 0,
      "classes": [
        "574f216afd487958cd69772a"
      ],
      "admin": [
        "574f20509f44e7531786c811",
        "57508a2a3a0a919c16ace3c0"
      ],
      "teacher": [
        "574f20f39f44e7531786c812",
        "575002b48188a3f821c2a66e",
        "57500bbaea09bc400d047bf6"
      ],
      "student": [
        "574f2d56590529c01a2a473b",
        "574f2e5842c5885b1b1729ab",
        "574f2ed542c5885b1b1729ae",
        "574f2f57555210991bf66e07",
        "574f2fcd087809b11bd8d5e4",
        "574f301d1f5025d61b7392b6",
        "574f30481d02afff1bb00c71",
        "574f30b01d02afff1bb00c74",
        "574f310038136b3d1cf31b96"
      ]
    }

我的猫鼬查询

app.services._chkAdminSchool = function(payload){
        var deferred = q.defer();
        School
            //.find({ teacher:{$in:['574f20f39f44e7531786c812']}})

            .find({_id : "574f1f979f44e7531786c80f",admin:{"$in":[Object("57508a2a3a0a919c16ace3c0")]}})
            //.select(filers)
            .exec(function(error, record) {
                if (error) {
                    deferred.reject({
                        status:404,
                        message: 'Error in API',
                        error: error
                    });
                } else {
                    if(record.length === 0){
                        deferred.reject({
                            message: 'Admin and school doesnot match',
                            data: record
                        });
                    }else{
                        deferred.resolve({
                            message: 'Successfully get Admin',
                            data: record
                        });
                    }

                }
            });
        return deferred.promise;
    }

记录返回空数组。 在此先感谢您的帮助

【问题讨论】:

  • 'var SchoolSchema = new Schema({ name: {type: String, required: true, unique: true}, branch: {type: String}, location: {type: String}, block: {type: Boolean}, webURL: {type: String}, created_at: {type: Number}, updated_at: {type: Number}, 学生: {type:Array}, 老师: {type:Array}, admin: {type :Array},类:{type:Array} });'
  • 这是我的架构,我尝试了 ObjectID 和简单的 id 字符串来查找管理员。但它不工作

标签: node.js mongodb mongoose


【解决方案1】:

您使用的查询对我来说工作正常并返回对象,请确保您在 Schema 中定义管理字段,如下所示:

"admin": [{type: mongoose.Schema.ObjectId }]

避免管理数组对象是字符串。如果您将Object("57508a2a3a0a919c16ace3c0")"57508a2a3a0a919c16ace3c0" 进行比较,它将返回空数组,请检查并重播给我,谢谢。

【讨论】:

    【解决方案2】:

    我不确定您是如何定义 School Schema 的,但如果您将 id 保存在 admin 数组中作为字符串,则无需在 mongoose 查询中更改为 Object。

    School.find({
      _id: "574f1f979f44e7531786c80f",
      admin: {
        $in : ['57508a2a3a0a919c16ace3c0']
      }
    }, functin (err, record) {
    ...
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-03-06
      • 1970-01-01
      • 2013-09-15
      • 1970-01-01
      • 2017-01-09
      • 2021-04-07
      • 1970-01-01
      • 2021-04-19
      相关资源
      最近更新 更多