Express +Mongoose 将对象推入对象数组并在插入时向对象添加两行
Express +Mongoose push object into array of objects and adding two more rows into the object while inserting
我想在数组对象中添加一个用户,并在插入时再添加两行。
这是使用的两个猫鼬模型。
module.exports = mongoose.model('Users', {
id:String, //the same id as user.id
nick:String, //the same nick as user.nick
});
module.exports = mongoose.model('Stores', {
id: String,
user: [{
id: String,
nick: String,
nr: Number,
earnings: Number
}],
total: Number
});
假设我想插入一个通过其 ID(不是自动生成的)找到的用户。 (我删除了 if (err) 以使其可读)。
这就是我现在尝试解决的方法。
Users.findOne({id : req.body.userid }, function(err, user) {
//what user contains
user = { _id: 551fb0b688eacdf0e700000c,
id: '123abc',
nick: 'Foo',
__v: 0 }
//I want to add this into the user and push it into exsisting array of
objects that is 'users'
//But before that i want to add more info to the user,
//the desired format that I want to insert into 'users'
user = {id: '123abc',
nick: 'Foo',
nr: req.body.nr, //new
earnings: req.body.earnings} //new
Stores.update({id: req.params.id},
{$push: { 'users' : user }}, function(err, store) {
});
});
The current result is the following.
users: [
{
id: "123abc",
nick: "Foo"
}]
我该如何解决这个问题?
照原样设计的架构至少会产生一个问题。如果用户更新他们的 nick
怎么办?您不仅需要更新 Users
集合,还需要更新 Stores
中与用户匹配的每个文档。您可以使用 ref
,然后使用 populate
来消除这种担忧。
module.exports = mongoose.model('Users', {
id: String, //the same id as user.id
nick: String, //the same nick as user.nick
});
module.exports = mongoose.model('Stores', {
id: String,
users: [{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users'
},
nr: Number,
earnings: Number
}],
total: Number
});
现在查询将是:
Users.findOne({
id: req.body.userid
}, function(err, user) {
Stores.update({
id: req.params.id
}, {
$push: {
'users': {
user: user,
nr: req.body.nr, //new
earnings: req.body.earnings
}
}
}, function(err, store) {});
});
以后需要查询的时候Stores
:
Stores
.find(QUERY)
.populate('users')
.exec(function(err, stores) {...
});
我想在数组对象中添加一个用户,并在插入时再添加两行。
这是使用的两个猫鼬模型。
module.exports = mongoose.model('Users', {
id:String, //the same id as user.id
nick:String, //the same nick as user.nick
});
module.exports = mongoose.model('Stores', {
id: String,
user: [{
id: String,
nick: String,
nr: Number,
earnings: Number
}],
total: Number
});
假设我想插入一个通过其 ID(不是自动生成的)找到的用户。 (我删除了 if (err) 以使其可读)。 这就是我现在尝试解决的方法。
Users.findOne({id : req.body.userid }, function(err, user) {
//what user contains
user = { _id: 551fb0b688eacdf0e700000c,
id: '123abc',
nick: 'Foo',
__v: 0 }
//I want to add this into the user and push it into exsisting array of
objects that is 'users'
//But before that i want to add more info to the user,
//the desired format that I want to insert into 'users'
user = {id: '123abc',
nick: 'Foo',
nr: req.body.nr, //new
earnings: req.body.earnings} //new
Stores.update({id: req.params.id},
{$push: { 'users' : user }}, function(err, store) {
});
});
The current result is the following.
users: [
{
id: "123abc",
nick: "Foo"
}]
我该如何解决这个问题?
照原样设计的架构至少会产生一个问题。如果用户更新他们的 nick
怎么办?您不仅需要更新 Users
集合,还需要更新 Stores
中与用户匹配的每个文档。您可以使用 ref
,然后使用 populate
来消除这种担忧。
module.exports = mongoose.model('Users', {
id: String, //the same id as user.id
nick: String, //the same nick as user.nick
});
module.exports = mongoose.model('Stores', {
id: String,
users: [{
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Users'
},
nr: Number,
earnings: Number
}],
total: Number
});
现在查询将是:
Users.findOne({
id: req.body.userid
}, function(err, user) {
Stores.update({
id: req.params.id
}, {
$push: {
'users': {
user: user,
nr: req.body.nr, //new
earnings: req.body.earnings
}
}
}, function(err, store) {});
});
以后需要查询的时候Stores
:
Stores
.find(QUERY)
.populate('users')
.exec(function(err, stores) {...
});