find id of latest subdocument inserted in mongoose

前端 未结 4 1539
自闭症患者
自闭症患者 2020-12-15 21:21

i have a model schema as :

var A = new Schema ({
  a: String,
  b : [ { ba: Integer, bb: String } ]
}, { collection: \'a\' } );

then

<
相关标签:
4条回答
  • 2020-12-15 21:32

    Mongoose will automatically create an _id for each new sub document, but - as far as I know - doesn't return this when you save it.

    So you need to get it manually. The save method will return the saved document, including the subdocs. As you're using push you know it will be the last item in the array, so you can access it from there.

    Something like this should do the trick.

    m['b'].push({ba:235,bb:"World"});
    m.save(function(err,model){
      // model.b is the array of sub documents
      console.log(model.b[model.b.length-1].id);
    });
    
    0 讨论(0)
  • 2020-12-15 21:41

    If you have a separate schema for your subdocument, then you can create the new subdocument from a model before you push it on to your parent document and it will have an ID:

    var bSchema = new mongoose.Schema({
      ba: Integer,
      bb: String
    };
    
    var a = new mongoose.Schema({
      a: String,
      b : [ bSchema ]
    });
    
    var bModel = mongoose.model('b', bSchema);
    var subdoc = new bModel({
      ba: 5,
      bb: "hello"
    });
    
    console.log(subdoc._id);    // Voila!
    

    Later you can add it to your parent document:

    m['b'].push(subdoc)
    m.save(...
    
    0 讨论(0)
  • 2020-12-15 21:43

    The question is "a bit" old, but what I do in this kind of situation is generate the subdocument's id before inserting it.

    var subDocument = {
        _id: mongoose.Types.ObjectId(),
        ba:235,
        bb:"World"
    };
    
    m['b'].push(subDocument);
    
    m.save(function(err,model){
      // I already know the id!
      console.log(subDocument._id);
    });
    

    This way, even if there are other database operations between the save and the callback, it won't affect the id already created.

    0 讨论(0)
  • 2020-12-15 21:52

    I've been looking for this answer as well, and I'm not sure that I like accessing the last document of the array. I do have an alternative solution, however. The method m['b'].push will return an integer, 1 or 0 - I'm assuming that is based off the success of the push (in terms of validation). However, in order to get access to the subdocument, and particularly the _id of the subdocument - you should use the create method first, then push.

    The code is as follows:

    var subdoc = m['b'].create({ ba: 234, bb: "World" });
    m['b'].push(subdoc);
    console.log(subdoc._id);
    m.save(function(err, model) { console.log(arguments); });
    

    What is happening is that when you pass in the object to either the push or the create method, the Schema cast occurs immediately (including things like validation and type casting) - this means that this is the time that the ObjectId is created; not when the model is saved back to Mongo. In fact, mongo does not automatically assign _id values to subdocuments this is a mongoose feature. Mongoose create is documented here: create docs

    You should also note therefore, that even though you have a subdocument _id - it is not yet in Mongo until you save it, so be weary of any DOCRef action that you might take.

    0 讨论(0)
提交回复
热议问题