MongoDB bulkWrite()使用范例


  • Lv 1

    bulkWrite()方法

    在SQL server 中,我们可以使用下面一系列的语句,或者是写成sp的方式,来进行批处理,在MongoDB中也有类似的语法bulkWrite

    *以下位于SQL server中,进行批处理仅需将语句按顺序写下去:

    insert into temp(id,name,phone,age) values(1,'John','111111111',25)
    insert into temp(id,name,phone,age) values(2,'Wang','222222222',15)
    insert into temp(id,name,phone,age) values(3,'Jane','333333333',18)
    insert into temp(id,name,phone,age) values(4,'Sue','444444444',33)
    update temp set phone='999999999' where id=1
    delete temp where id=2
    update temp set name='Susan',phone='123456789',age=22 where id=4
    

    *以下为相对应于MongoDB中,进行批处理的语句:

    try 
    { 
        db.temp.bulkWrite
        ( 
        [ 
            { insertOne : { "document" : { "_id" : 1, "name" : "John", "phone" : "111111111", "age" : 25 } } }, 
            { insertOne : { "document" : { "_id" : 2, "name" : "Wang", "phone" : "222222222", "age" : 15 } } },
            { insertOne : { "document" : { "_id" : 3, "name" : "Jane", "phone" : "333333333", "age" : 18 } } },
            { insertOne : { "document" : { "_id" : 4, "name" : "Sue", "phone" : "444444444", "age" : 33 } } }, 
            { updateOne : { "filter" : { "_id" : 1 }, "update" : { $set : { "phone" : "999999999" } } } }, 
            { deleteOne : { "filter" : { "_id" : 2 } } }, 
            { replaceOne : { "filter" : { "_id" : 4 }, "replacement" : { "name" : "Susan", "phone" : "123456789", "age" : 32 } } } 
        ] 
        ); 
    } 
    catch (e) { print(e); }

登录后回复
 

与 萌阔论坛 的连接断开,我们正在尝试重连,请耐心等待