Mongodb Mongoose-使用findOneAndUpdate更新子文档

Mongodb Mongoose-使用findOneAndUpdate更新子文档,mongodb,reactjs,express,mongoose,redux,Mongodb,Reactjs,Express,Mongoose,Redux,我不知道具体的方法是不是行不通。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

我不知道具体的方法是不是行不通。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"
}
但子文档肯定存在于数据库中:

{
    "_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);

您可以在
打开位置
嵌入式数组中查询
\u id
字段。因此,在查询部分中,您必须引用
open\u positions
,后跟其id

当您在查询部分中使用数组引用时,您会得到占位符
$
位置运算符,该运算符引用了匹配数组元素的索引,并且在更新部分Mongo使用以前找到的数组索引替换
$
,然后使用
更新
文档替换数组元素

使用


你能试试
Project.findOneAndUpdate({“open\u positions.\u id:{“open\u positions.$”),{“open\u positions.$”:update},{new:true}…
?真是糟透了。刚才发生了什么事?它是怎么工作的?
Project.findOneAndUpdate({"open_positions._id" : _id}, { "open_positions.$" : update }, { new: true }....