我不知道具体方法是否有效。 findOneAndUpdate应该与子文档一起使用吗?
使用react / redux / express。
路线:
app.route("/api/positions/:position_id").put(function(req, res) {
const _id = req.params.position_id;
const update = req.body;
process.nextTick(function() {
// for each element in req.body -> I will update the correct field
Project.findOneAndUpdate({_id : _id}, update, { new: true }, function(
err,
updatedDoc
) {
if (err)
res.send(err);
else if (!updatedDoc)
res.json({ message: "No Position Found", _id });
else {
console.log(updatedDoc);
res.json({
message: "Update Successful",
updatedDoc
}); }
});
});
});
邮递员回复:{
"message": "No Position Found",
"_id": "58d1908861f3513dc0c85f7d"
}
但是子文档肯定存在于DB中:
{
"_id": {
"$oid": "58c32c197f37c62a944f0ad2"
},
"name": "One",
"phrase": "Two",
"description": "Three",
"userID": "58ac8e0ec1d5dc37043b8a7f",
"open_positions": [
{
"position_title": "Master Chief",
"position_description": "Superman chief",
"position_tags": [
"Healthcare",
"Finance",
"Consumer Products"
]
},
{
"position_title": "Master Chief",
"position_description": "Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor Lorem Ipsum Dolor ",
"position_tags": [
"Healthcare",
"Finance",
"Consumer Products",
"Healthcare",
"Finance",
"Consumer Products",
"Healthcare",
"Finance",
"Consumer Products"
]
},
{
"position_description": "please join us we are the best",
"_id": {
"$oid": "58d18fb8b1f3272de8b89158"
},
"position_tags": [
""
]
},
{
"position_description": "please join us we are the best",
"_id": {
"$oid": "58d18fc9d993233df4e52ace"
},
"position_tags": [
""
]
},
{
"position_description": "please join us we are the best",
"_id": {
"$oid": "58d18fdcd993233df4e52acf"
},
"position_tags": [
""
]
},
{
"position_title": "BEST POSITION",
"position_description": "please join us we are the best",
"_id": {
"$oid": "58d18ffbd993233df4e52ad0"
},
"position_tags": [
""
]
},
{
"position_title": "BEST POSITION",
"position_description": "please join us we are the best",
"_id": {
"$oid": "58d1908861f3513dc0c85f7d"
},
"position_tags": [
""
]
}
],
"fields": [
"Healthcare",
"Finance",
"Consumer Products"
],
"skills": [],
"dateAdded": {
"$date": "2017-03-10T22:43:37.544Z"
},
"size": "2-5",
"stage": "development",
"visible": true,
"__v": 5
}
我做错了什么?
编辑:
如果它有帮助,这是架构:
// schema for open positions in a project
var positionSchema = mongoose.Schema({
position_title: String,
position_description: String,
position_tags: Array,
archived: Boolean,
archivedDate: Date
});
// schema for single project data
var projectSchema = mongoose.Schema({
userID: String,
name: String,
phrase: String,
visible: {
type: Boolean,
default: true
},
positions: Boolean,
stage: {
type: String,
default: "Idea"
},
size: {
type: String,
default: "1"
},
members: String,
dateAdded: {
type: Date,
default: Date.now
},
description: String,
skills: Array,
fields: {
type: Array,
default: ["Other"]
},
open_positions: [positionSchema],
archived: Boolean,
archivedDate: Date
});
// expose schema to app
module.exports = mongoose.model("Project", projectSchema);
答案 0 :(得分:1)
您查询_id
嵌入式数组中的open_positions
字段。因此,在查询部分中,您可以引用open_positions
后跟其ID。
在查询部分中使用数组引用时,您将获得占位符$
位置运算符,该运算符引用匹配数组元素的索引,在更新部分中,Mongo将$
替换为先前找到的数组索引后跟用update
doc。
使用
Project.findOneAndUpdate({"open_positions._id" : _id}, { "open_positions.$" : update }, { new: true }....