mongodb 查询要点说明

来源:互联网 发布:靠谱的日本代购淘宝店 编辑:程序博客网 时间:2024/06/05 13:29

1.find()

 var cursor = collection.find(query, [fields], options);
 cursor.sort(fields).limit(n).skip(m).    cursor.nextObject(function(err, doc) {});    cursor.each(function(err, doc) {});    cursor.toArray(function(err, docs) {});    cursor.rewind()  // reset the cursor to its initial state.


2.insert()

    collection.insert(docs, options, [callback]);

Useful options:(*)

  • safe:true Should always set if you have a callback.
注:如果你要写一个callback回调函数,则必须设置{safe:true},否则回调函数不起作用

3.update()

collection.update(criteria, objNew, options, [callback]);

Useful options:(*)

  • safe:true Should always set if you have a callback.
  • multi:true If set, all matching documents are updated, not just the first.
  • upsert:true Atomically inserts the document if no documents matched.

4.findAndModify()

  

Useful options:(*)

  • remove:true set to a true to remove the object before returning
  • new:true set to true if you want to return the modified object rather than the original. Ignored for remove.
  • upsert:true Atomically inserts the document if no documents matched.

原创粉丝点击