简体   繁体   中英

How to create users from array and return their ids in mongoose?

Service get array of users' names, check every name if user with this name exists(if not then create user) and return ids of users with such names. I don't know how to do it because search and save are async.

我使用节点js的异步库来实现。

Following code is for query the collection to get a document based on id .if its found it returns the doc other wise it can inseret the doc for that we used {upsert:true}.this is basic code sample follow mongoose docs http://mongoosejs.com/docs/api.html

Model.findOneAndUpdate({_id:id},{upsert:true}, function(err, doc){
    if (err)  {
        console.log('if error occurs'); 
    } else {
        console.log('doc',doc) 
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM