【问题标题】:Mongo db, how to give object _id another collection's documentMongodb,如何给对象_id另一个集合文档
【发布时间】:2017-05-06 02:56:17
【问题描述】:

我有 2 个集合,称为用户和位置。在用户中,有一个位置_id,这是一个Object。 Id 还引用位置集合。我的问题是我做错了什么?当我调用 getUser 函数时,我想查看用户信息和用户的位置信息。我需要做什么?

用户架构

    module.exports = (function userSchema() {
    var Mongoose = require('mongoose');

    var userSchema = Mongoose.Schema({
        name: {
            type: String,
            require: true
        },
        surname: {
            type: String,
            require: true
        },
        tel: {
            type: String,
            require: true
        },
        age: {
            type: String,
            require: true
        },
        mevki_id: {
            type: String,
            require: true
        },
        location_id: [{
            type: Mongoose.Schema.Types.ObjectId,
            ref: 'locations'
        }]
    });

    var collectionName = 'users';

    var User = Mongoose.model(collectionName, userSchema);

    return User;
})();

用户控制器

    function userController() {
    var User = require('../models/UserSchema');

    this.createUser = function (req, res, next) {
        var name = req.body.name;
        var surname = req.body.surname;
        var tel = req.body.tel;
        var age = req.body.age;
        var mevki_id = req.body.mevki_id;
        var lok_id = req.body.lok_id;

        User.create({
            name: name,
            surname: surname,
            tel: tel,
            age: age,
            mevki_id: mevki_id,
            lok_id: lok_id
        }, function (err, result) {
            if (err) {
                console.log(err);
                return res.send({
                    'error': err
                });
            } else {
                return res.send({
                    'result': result,
                    'status': 'successfully saved'
                });
            }
        });
    };

    this.getUser = function (req, res, next) {
        User.find()
            .populate('lok_id')
            .exec(function (err, result) {
                if (err) {
                    console.log(err);
                    return res.send({
                        'error': err
                    });
                } else {
                    return res.send({
                        'USERS': result
                    });
                }
            });
    };
    return this;
};

module.exports = new UserController();

【问题讨论】:

    标签: node.js mongodb collections reference schema


    【解决方案1】:

    首先,您的schema 是错误的:

    var userSchema = new Mongoose.Schema({
      // ...
      location_id: { type: [Mongoose.Schema.Types.ObjectId], ref: 'locations' }
    })
    

    其次,在您的schema 中,最后一个字段名称是location_id,而在您的控制器中,您将其更改为lok_id

    所以,解决这个问题:

    User.create({
      // ...
      location_id: lok_id
    }
    

    还有这个:

    User
      .find()
      .populate('location_id')
    

    更新 在您的 json 中,最后一个字段名称是 location_id,因此,也要修复这个问题:

    this.createUser = function (req, res, next) {
      // ...
      var lok_id = req.body.location_id;
    }
    

    【讨论】:

    • 我真的很感谢你的帮助,但实际上它不起作用我无法创建用户。我的 json 文件有什么问题吗:
    • 你能检查我更新的答案吗?如果它仍然不起作用,您能否显示console.log(req.body)的结果?
    • { "name": "Akif", "surname": "Demirezen", "tel": "6056056", "age": "35", "mevki_id":"2", " location_id": { "il":"istanbul", "ilce":"beylikduzu" } } 我像上面一样发布 json 文件,但结果是这样的:{ "result": { "__v": 0, "_id": "5859f736cc66d64394141b2e ", "location_id": [] }, "status": "成功保存" }
    • location_id 应该是 ObjectId 的字符串数组,而不是对象。例如,"location_id": ["5859f736cc66d64394141b2a", "5859f736cc66d64394141b2b", "5859f736cc66d64394141b2c"].
    • 是的,但是猫鼬必须将此对象 id 生成到位置 id 中。正确的?我认为我的 createUser 函数是错误的。我还需要在创建用户函数中创建位置。
    猜你喜欢
    • 2014-03-19
    • 2015-01-18
    • 2011-04-30
    • 1970-01-01
    • 2019-07-10
    • 2021-02-22
    相关资源
    最近更新 更多