0
特定の方法がうまくいかないかどうかわかりません。 findOneAndUpdateはサブ文書を操作する必要がありますか?Mongoose - 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
}
?
EDIT:
// 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);
あなたはProject.findOneAndUpdate({ "open_positions._id":_id} '試すことができ、{ " open_positions $":更新}、{新しい:真} ....'? – Veeram
うんざりしていた、うんざりしたことは何ですか?どのように機能しましたか? – user3599920