Get the identifier of the affected document using update ()

I use this to do upsert:

Articles.update(
    { title: title },
    { 
        title: title,
        parent: type
    },
    { upsert: true }, 
    function(res) {
        return console.log(res);
    }
);

console.log(needToGetID);

Now I need to get the _id of the document that has been updated or inserted. I thought I could get this through a callback, but res- undefined. I assume that there is only one unique document that is defined by the request.

Update

Forgot to mention that I use a meteorite ...

+4
source share
1 answer

.update() , "" () ( "multi" ) , , , "" , .

, " ", .findOneAndUpdate() , mongoose:

Articles.findOneAndUpdate(
    { title: title },
    {
        title: title,
        parent: type
    },
    { upsert: true, new: true  }, 
    function(res) {
        return console.log(res);
    }
);

new: true, , , "", .

, - , , _id .


meteor , .findAndModify(), :

meteor add fongandrew:find-and-modify

:

Articles.findAndModify(
    {
        "query": { title: title },
        "update": {
            title: title,
            parent: type
        },
        "upsert": true, 
        "new": true  
    }, 
    function(res) {
        return console.log(res);
    }
);

, $set, "" :

Articles.findAndModify(
    {
        "query": { "title": title },
        "update": {
            "$set": {
                "parent": type
            }
        },
        "upsert": true, 
        "new": true  
    }, 
    function(res) {
        return console.log(res);
    }
);
+4

Source: https://habr.com/ru/post/1608212/


All Articles