Node.js Mongo db,如何给对象_id另一个集合';s文件
我有两个集合,称为用户和位置。在用户中,有一个位置id,这是一个Node.js Mongo db,如何给对象_id另一个集合';s文件,node.js,mongodb,collections,reference,schema,Node.js,Mongodb,Collections,Reference,Schema,我有两个集合,称为用户和位置。在用户中,有一个位置id,这是一个对象。Id还引用位置集合。我的问题是我做错了什么?调用getUser函数时,我希望看到用户信息和用户的位置信息。我需要做什么 用户模式 module.exports = (function userSchema() { var Mongoose = require('mongoose'); var userSchema = Mongoose.Schema({ name: {
对象
。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();
首先,您的
模式
是错误的:
var userSchema = new Mongoose.Schema({
// ...
location_id: { type: [Mongoose.Schema.Types.ObjectId], ref: 'locations' }
})
其次,在模式中
最后一个字段名是location\u id
,而在控制器中,将其更改为lok\u id
所以,解决这个问题:
User.create({
// ...
location_id: lok_id
}
这是:
User
.find()
.populate('location_id')
更新
在您的json
中,最后一个字段名是location\u id
,因此,也要解决这个问题:
this.createUser = function (req, res, next) {
// ...
var lok_id = req.body.location_id;
}
首先,您的
模式
是错误的:
var userSchema = new Mongoose.Schema({
// ...
location_id: { type: [Mongoose.Schema.Types.ObjectId], ref: 'locations' }
})
其次,在模式中
最后一个字段名是location\u id
,而在控制器中,将其更改为lok\u id
所以,解决这个问题:
User.create({
// ...
location_id: lok_id
}
这是:
User
.find()
.populate('location_id')
更新
在您的json
中,最后一个字段名是location\u id
,因此,也要解决这个问题:
this.createUser = function (req, res, next) {
// ...
var lok_id = req.body.location_id;
}
我真的很想你的帮助,但它不工作,实际上我不能创建用户。我的json文件有问题吗:你能检查一下我更新的答案吗?如果它仍然不起作用,您能否显示console.log(req.body)?{“name”:“Akif”,“姓氏”:“Demirezen”,“tel”:“6056056”,“age”:“35”,“mevki_id”:“2”,“location_id”:{“il”:“伊斯坦布尔”,“ilce”:“beylikduzu”}的结果是:{“result”:{:0,“\u id”:“5859F736CC66D643941B2E”,“location\u id”:[]},“status”:“successfully saved”},
location\u id
应该是ObjectId的字符串数组,而不是对象。例如,“location_id”:[“5859F736CC66D6439141B2A”,“5859F736CC66D6439141B2B”,“5859F736CC66D6439141B2C”]
。是的,但mongoose必须将此对象id生成到location id中。对吗?我认为我的createUser函数是错误的。我还需要在“创建用户”功能中创建位置。我真的很想得到你的帮助,但它不起作用,实际上我无法创建用户。我的json文件有问题吗:你能检查一下我更新的答案吗?如果它仍然不起作用,您能否显示console.log(req.body)?{“name”:“Akif”,“姓氏”:“Demirezen”,“tel”:“6056056”,“age”:“35”,“mevki_id”:“2”,“location_id”:{“il”:“伊斯坦布尔”,“ilce”:“beylikduzu”}的结果是:{“result”:{:0,“\u id”:“5859F736CC66D643941B2E”,“location\u id”:[]},“status”:“successfully saved”},location\u id
应该是ObjectId的字符串数组,而不是对象。例如,“location_id”:[“5859F736CC66D6439141B2A”,“5859F736CC66D6439141B2B”,“5859F736CC66D6439141B2C”]
。是的,但mongoose必须将此对象id生成到location id中。对吗?我认为我的createUser函数是错误的。我还需要在createuser函数中创建位置。