我无法“插入”我的阵列。下面的代码在我的answers
数组中创建了重复项,我绝对不希望这样做,并且现在看来$push
无法正常工作。我尝试使用在SO上看到的不同方法已有一段时间了,但是没有一种方法适合我。使用此网络应用程序,用户可以在网站上查看问题并以"is"或“否”的response
进行响应,并且他们可以随时更改(更新)其response
,这意味着在数据库在不同的时间。如何解决这个问题?
var QuestionSchema = Schema ({
title :String,
admin :{type: String, ref: 'User'},
answers :[{type: Schema.Types.Mixed, ref: 'Answer'}]
});
var AnswerSchema = Schema ({
_question :{type: ObjectId, ref: 'Question'},
employee :{type: String, ref: 'User'},
response :String,
isAdmin :{type: Boolean, ref: 'User'}
})
var UserSchema = Schema({
username : String,
isAdmin : {type: Boolean, default: false}
});
module.exports = mongoose.model('Question', QuestionSchema);
module.exports = mongoose.model('Answer', AnswerSchema);
module.exports = mongoose.model('User', UserSchema);
Question.update(
{_id: req.body.id},
{$push: {answers: {_question: req.body.id,
employee: req.body.employee,
response: req.body.response, //this variable changes (yes/no/null)
isAdmin: req.body.isAdmin}}},
{safe: true, upsert: true},
function(err, model) {
}
);
最佳答案
正如我所看到的,您似乎有些困惑,它反射(reflect)在您的模式中。您似乎没有完全掌握“嵌入式”和“引用”之间的区别,因为您的架构实际上是这两种技术的无效“混搭”。
可能最好将它们引导给您。
嵌入式模型
因此,实际上您应该拥有类似以下内容,而不是您已定义的模式:
var QuestionSchema = Schema ({
title :String,
admin :{type: String, ref: 'User'},
answers :[AnswerSchema]
});
var AnswerSchema = Schema ({
employee :{type: String, ref: 'User'},
response :String,
isAdmin :{type: Boolean, ref: 'User'}
})
mongoose.model('Question', questionSchema);
注意:此处
Question
是唯一的实际模型。 AnswerSchema
完全“嵌入”。注意“schema”的明确定义,其中
"answers"
中的Question
属性定义为AnswerSchema
的“数组”。这是您进行嵌入并保持对数组内部对象内类型的控制的方式。至于更新,有一个明确的逻辑模式,但是您根本没有执行它。您需要做的就是“告诉”您不想“推送”新项目的更新,如果该数组中已经存在该“唯一”
"employee"
的内容。还。这是,而不是和“upsert”。 Upsert暗示“创建一个新的”,这与您想要的不同。您想“推送”到“现有”问题的数组。如果您在此处保留“upsert”,那么找不到的内容将创建一个新的Question。这当然是错误的。
Question.update(
{
"_id": req.body.id,
"answers.employee": { "$ne": req.body.employee },
}
},
{ "$push": {
"answers": {
"employee": req.body.employee,
"response": req.body.response,
"isAdmin": req.body.isAdmin
}
}},
function(err, numAffected) {
});
这将检查数组成员中的“唯一”
"employee"
是否已经存在,并且仅在尚未存在的 $push
中进行检查。另外,如果您打算允许用户“更改他们的答案”,那么我们可以使用
.bulkWrite()
来实现:Question.collection.bulkWrite(
[
{ "updateOne": {
"filter": {
"_id": req.body.id,
"answers.employee": req.body.employee,
},
"update": {
"$set": {
"answers.$.response": req.body.response,
}
}
}},
{ "updateOne": {
"filter": {
"_id": req.body.id,
"answers.employee": { "$ne": req.body.employee },
},
"update": {
"$push": {
"answers": {
"employee": req.body.employee,
"response": req.body.response,
"isAdmin": req.body.isAdmin
}
}
}
}}
],
function(err, writeResult) {
}
);
这实际上将两个更新合二为一。第一个尝试更改现有答案,并在匹配的位置
$set
响应,第二个尝试在问题上找不到答案的地方添加新答案。引用模型
使用“引用”模型,您实际上将
Answer
的真实成员包含在自己的集合中。因此,架构的定义如下:var QuestionSchema = Schema ({
title :String,
admin :{type: String, ref: 'User'},
answers :[{ type: Schema.Types.ObjectId, ref: 'Answer' }]
});
var AnswerSchema = Schema ({
_question :{type: ObjectId, ref: 'Question'},
employee :{type: String, ref: 'User'},
response :String,
isAdmin :{type: Boolean, ref: 'User'}
})
mongoose.model('Answer', answerSchema);
mongoose.model('Question', questionSchema);
N.B 其他引用在这里是
User
的,例如: employee :{type: String, ref: 'User'},
isAdmin :{type: Boolean, ref: 'User'}
这些也确实是不正确的,也应该是
Schema.Type.ObjectId
,因为它们将“引用” _id
的实际User
字段。但这实际上超出了所提问题的范围,因此,如果您在阅读完此书后仍然不理解该内容,则可以使用Ask a New Question,以便有人可以解释。关于其余的答案。不过,这就是模式的“一般”形状,重要的是
"ref"
“model”的'Anwser'
,该名称由注册名称组成。您可以选择仅将现代 Mongoose 版本中的"_question"
字段与“虚拟”一起使用,但是我现在跳过了“高级用法”,并通过Question
模型中的一系列“引用”使其保持简单。在这种情况下,由于
Answer
模型实际上位于其自己的“集合”中,因此该操作实际上变为“upserts”。当对给定的"employee"
id没有"_question"
响应时,我们只想“创建”。还用
Promise
链演示:Answer.update(
{ "_question": req.body.id, "employee": req.body.employee },
{
"$set": {
"response": req.body.reponse
},
"$setOnInsert": {
"isAdmin": req.body.isAdmin
}
},
{ "upsert": true }
).then(resp => {
if ( resp.hasOwnProperty("upserted") ) {
return Question.update(
{ "_id": req.body.id, "answers": { "$ne": resp.upserted[0]._id },
{ "$push": { "answers": resp.upserted[0]._id } }
).exec()
}
return;
}).then(resp => {
// either undefined where it was not an upsert or
// the update result from Question where it was
}).catch(err => {
// something
})
这实际上是一条简单的语句,因为“当匹配时”我们要使用请求的有效负载来更改
"response"
数据,并且实际上仅在“upserting”或“创建/插入”时才实际更改其他数据,例如"employee"
(它总是隐含在查询表达式()和"isAdmin"
中,这显然不随每个更新请求而改变,然后我们显式使用 $setOnInsert
,因此它仅在实际的“创建”中写入了这两个字段。在“ promise 链”中,我们实际上要查看对
Answer
的更新请求是否实际上导致了“upsert”,并且当它发出时,我们希望将其追加到尚不存在的Question
数组中。与“嵌入式”示例大致相同,最好在使用“更新”进行修改之前先查看数组是否确实具有该项目。或者,您可以在此处 $addToSet
,让查询通过Question
匹配_id
。但是对我来说,那是浪费的写作。概括
这些是您处理此问题的不同方法。每种都有自己的用例,您可以在以下方面看到我的一些其他答案的概述:
并非“必读”阅读,但它可以帮助您深入了解哪种方法最适合您的特定情况。
工作实例
复制这些文件并将它们放在目录中,然后执行
npm install
以安装本地依赖项。该代码将运行并在数据库中创建进行更改的集合。使用
mongoose.set(debug,true)
打开日志记录,因此您应该查看控制台输出并查看其操作,以及结果集合,在该集合中将记录对相关问题的答案,然后覆盖原意,而不是“复制”。如果需要,请更改连接字符串。但这就是您应为此目的在列表中更改的所有内容。答案中描述的两种方法都得到了证明。
package.json
{
"name": "colex",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"async": "^2.4.1",
"mongodb": "^2.2.29",
"mongoose": "^4.10.7"
}
}
index.js
const async = require('async'),
mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = require('mongodb').ObjectID
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
mongoose.connect('mongodb://localhost/coltest');
const userSchema = new Schema({
username: String,
isAdmin: { type: Boolean, default: false }
});
const answerSchemaA = new Schema({
employee: { type: Schema.Types.ObjectId, ref: 'User' },
response: String,
});
const answerSchemaB = new Schema({
question: { type: Schema.Types.ObjectId, ref: 'QuestionB' },
employee: { type: Schema.Types.ObjectId, ref: 'User' },
response: String,
});
const questionSchemaA = new Schema({
title: String,
admin: { type: Schema.Types.ObjectId, ref: 'User' },
answers: [answerSchemaA]
});
const questionSchemaB = new Schema({
title: String,
admin: { type: Schema.Types.ObjectId, ref: 'User' },
answers: [{ type: Schema.Types.ObjectId, ref: 'AnswerB' }]
});
const User = mongoose.model('User', userSchema);
const AnswerB = mongoose.model('AnswerB', answerSchemaB);
const QuestionA = mongoose.model('QuestionA', questionSchemaA);
const QuestionB = mongoose.model('QuestionB', questionSchemaB);
async.series(
[
// Clear data
(callback) => async.each(mongoose.models,(model,callback) =>
model.remove({},callback),callback),
// Create some data
(callback) =>
async.each([
{
"model": "User",
"object": {
"_id": "594a322619ddbd437193c759",
"name": "Admin",
"isAdmin": true
}
},
{
"model": "User",
"object": {
"_id": "594a323919ddbd437193c75a",
"name": "Bill"
}
},
{
"model": "User",
"object": {
"_id": "594a327b19ddbd437193c75b",
"name": "Ted"
}
},
{
"model": "QuestionA",
"object": {
"_id": "594a32f719ddbd437193c75c",
"admin": "594a322619ddbd437193c759",
"title": "Question A Model"
}
},
{
"model": "QuestionB",
"object": {
"_id": "594a32f719ddbd437193c75c",
"admin": "594a322619ddbd437193c759",
"title": "Question B Model"
}
}
],(data,callback) => mongoose.model(data.model)
.create(data.object,callback),
callback
),
// Submit Answers for Users - Question A
(callback) =>
async.eachSeries(
[
{
"_id": "594a32f719ddbd437193c75c",
"employee": "594a323919ddbd437193c75a",
"response": "Bills Answer"
},
{
"_id": "594a32f719ddbd437193c75c",
"employee": "594a327b19ddbd437193c75b",
"response": "Teds Answer"
},
{
"_id": "594a32f719ddbd437193c75c",
"employee": "594a323919ddbd437193c75a",
"response": "Bills Changed Answer"
}
].map(d => ([
{ "updateOne": {
"filter": {
"_id": ObjectId(d._id),
"answers.employee": ObjectId(d.employee)
},
"update": {
"$set": { "answers.$.response": d.response }
}
}},
{ "updateOne": {
"filter": {
"_id": ObjectId(d._id),
"answers.employee": { "$ne": ObjectId(d.employee) }
},
"update": {
"$push": {
"answers": {
"employee": ObjectId(d.employee),
"response": d.response
}
}
}
}}
])),
(data,callback) => QuestionA.collection.bulkWrite(data,callback),
callback
),
// Submit Answers for Users - Question A
(callback) =>
async.eachSeries(
[
{
"_id": "594a32f719ddbd437193c75c",
"employee": "594a323919ddbd437193c75a",
"response": "Bills Answer"
},
{
"_id": "594a32f719ddbd437193c75c",
"employee": "594a327b19ddbd437193c75b",
"response": "Teds Anwser"
},
{
"_id": "594a32f719ddbd437193c75c",
"employee": "594a327b19ddbd437193c75b",
"response": "Ted Changed it"
}
],
(data,callback) => {
AnswerB.update(
{ "question": data._id, "employee": data.employee },
{ "$set": { "response": data.response } },
{ "upsert": true }
).then(resp => {
console.log(resp);
if (resp.hasOwnProperty("upserted")) {
return QuestionB.update(
{ "_id": data._id, "employee": { "$ne": data.employee } },
{ "$push": { "answers": resp.upserted[0]._id } }
).exec()
}
return;
}).then(() => callback(null))
.catch(err => callback(err))
},
callback
)
],
(err) => {
if (err) throw err;
mongoose.disconnect();
}
)
关于node.js - 在不创建重复项的情况下使用阵列进行Upsert,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44660284/