gpt4 book ai didi

javascript - 合并 Mongoose 中的两个对象

转载 作者:可可西里 更新时间:2023-11-01 09:57:49 26 4
gpt4 key购买 nike

我在 mongoose 的 user.jsuserProfile.js 中有两个模型,我想在其中使用连接查询获取文档,但我没有采用 ref 在用户架构中,所以我有如下代码:

user.js

var userSchema = new Schema({
nick_name:{type:String},
email: {type: String},
password: {type: String},
is_active:{type:String,enum:['1','0'],default:"1"},
},{ collection: 'user'});

userProfile.js

var userProfileSchema = new Schema({
user_id:{type:Schema.Types.ObjectId, ref:'User'},
first_name:{type:String},
last_name:{type:String},
address:{type:String},
country:{type:String},
state:{type:String},
city:{type:String},
gender:{type:String,enum:['m','f','o'],default:"m"},
is_active:{type:String,enum:['1','0'],default:"1"},
},{ collection: 'userProfile'});

server.js

app.get('/api/userLists', function(req, res) {
User.find({},"nick_name email",function(err, user) {
if(err) {
res.json(err);
} else {
var userIds = user.map(function(obj){
return obj._id;
});

UserProfile.find({user_id:{$in:userIds}},function(err,userProfiles){
if(err){
res.json(userProfiles);
console.log(userProfiles);
}else{

---------------------------------
-What to do here, I have no idea.-
---------------------------------

res.json(user);
}
});

}
});

});

预期输出如下

{
"nick_name" : "laxman",
"email" : "laxman@mailinator.com",
"first_name" : "my first name",
"last_name" : "my last name",
"address" : "my address",
"country" : "my country",
"state" : "my state",
"city" : "my city",
"gender" : "m",
}

**OR**

{
"nick_name" : "laxman",
"email" : "laxman@mailinator.com",
"profile" :{
"first_name" : "my first name",
"last_name" : "my last name",
"address" : "my address",
"country" : "my country",
"state" : "my state",
"city" : "my city",
"gender" : "m",
}
}

依赖性

"express"  => "version": "4.7.4",
"mongoose" => "version": "4.4.5",
"mongodb" => "version": "2.4.9",
"OS" => "ubuntu 14.04 lts 32bit",

最佳答案

在您的情况下,人口将是理想的。来自 docs :

Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).

Population 将无缝地帮助您将 Profile 集合中的数据导入您的 User 模型。比如获取json形式的第二个输出

{
"nick_name": "laxman",
"email": "laxman@mailinator.com",
"profile": {
"first_name" : "my first name",
"last_name" : "my last name",
"address" : "my address",
"country" : "my country",
"state" : "my state",
"city" : "my city",
"gender" : "m",
}
}

考虑以下示例以及您可以采用的方法。将您的 User 架构更改为

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({
nick_name: { type: String },
email: { type: String },
password: { type: String },
profile: { type: Schema.Types.ObjectId, ref: 'UserProfile' }
},{ collection: 'user'});

module.exports = mongoose.model('User', userSchema);

在您的用户架构定义中,您将主代理键添加到名为 ObjectId 的用户配置文件中,在数据中引用为 _id 以获取填充功能。这将是用于引用 UserProfile 集合中文档的键。

读取数据

这是 Mongoose population 显示通过其 populate() 读取文档是多么直接、简单和快速的地方。 功能。因此,例如,要显示用户引用的 profile,请调用 populate() 以字符串中该字段的名称作为参数的方法,例如

app.get('/api/userLists', function(req, res) {
User.find({}, "nick_name email")
.populate("profile")
.exec(function(err, users) {
if(err) {
res.json(err);
} else {
res.json(users)
}
});

写入数据

当您为user 模型保存数据时,您还需要保存对配置文件的引用。例如,创建新的 User 时,您需要将 UserProfile _id 引用保存为配置文件字段:

var user = new User();
var profileId = "54b5659536cd5250a3a93bd3"; // <-- user profile id, for example.
user.nick_name = req.body.name;
user.email = req.body.email;
user.password = encrypt(req.body.password, user.nick_name);
user.profile = profileId;

user.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'user created!' });
});

关于javascript - 合并 Mongoose 中的两个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35985664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com