当前位置:Gxlcms > 数据库问题 > MongoDB CRUD之D

MongoDB CRUD之D

时间:2021-07-01 10:21:17 帮助过:3人阅读

<filter>, { writeConcern: <document>, collation: <document> } )

删除与筛选器匹配的第一个文档。用于 capped collection 时会抛出一个WriteError异常。

  1. <code>> db.orders.find().pretty()
  2. {
  3. "_id" : ObjectId("563237a41a4d68582c2509da"),
  4. "stock" : "Brent Crude Futures",
  5. "qty" : 250,
  6. "type" : "buy-limit",
  7. "limit" : 48.9,
  8. "creationts" : ISODate("2015-11-01T12:30:15Z"),
  9. "expiryts" : ISODate("2015-11-01T12:35:15Z"),
  10. "client" : "Crude Traders Inc."
  11. }
  12. > try {
  13. ... db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } );
  14. ... } catch (e) {
  15. ... print(e);
  16. ... }
  17. { "acknowledged" : true, "deletedCount" : 1 }
  18. > db.orders.find().pretty()
  19. ></code>

删除日期比较早的

  1. <code>> db.orders.find().pretty()
  2. {
  3. "_id" : ObjectId("563237a41a4d68582c2509da"),
  4. "stock" : "Brent Crude Futures",
  5. "qty" : 250,
  6. "type" : "buy-limit",
  7. "limit" : 48.9,
  8. "creationts" : ISODate("2015-11-01T12:30:15Z"),
  9. "expiryts" : ISODate("2015-11-01T12:35:15Z"),
  10. "client" : "Crude Traders Inc."
  11. }
  12. > try {
  13. ... db.orders.deleteOne( { "expiryts" : { $lt: ISODate("2015-11-01T12:40:15Z") } } );
  14. ... } catch (e) {
  15. ... print(e);
  16. ... }
  17. { "acknowledged" : true, "deletedCount" : 1 }
  18. > db.orders.find().pretty()
  19. > </code>

deleteMany

格式

  1. <code class="sourceCode json"><span class="er">db.collection.deleteMany(</span>
  2. <span class="er"><filter>,</span>
  3. <span class="fu">{</span>
  4. <span class="er">writeConcern</span><span class="fu">:</span> <span class="er"><document></span><span class="fu">,</span>
  5. <span class="er">collation</span><span class="fu">:</span> <span class="er"><document></span>
  6. <span class="fu">}</span>
  7. <span class="er">)</span></code>

用于 capped collection 时会抛出一个WriteError异常。

  1. <code>> db.orders.find().pretty()
  2. {
  3. "_id" : ObjectId("563237a41a4d68582c2509da"),
  4. "stock" : "Brent Crude Futures",
  5. "qty" : 250,
  6. "type" : "buy-limit",
  7. "limit" : 48.9,
  8. "creationts" : ISODate("2015-11-01T12:30:15Z"),
  9. "expiryts" : ISODate("2015-11-01T12:35:15Z"),
  10. "client" : "Crude Traders Inc."
  11. }
  12. > try {
  13. ... db.orders.deleteMany( { "client" : "Crude Traders Inc." } );
  14. ... } catch (e) {
  15. ... print (e);
  16. ... }
  17. { "acknowledged" : true, "deletedCount" : 1 }
  18. > db.orders.find().pretty()
  19. ></code>

其实跟deleteOne差不多

remove

格式

  1. <code>db.collection.remove(
  2. <query>,
  3. <justOne>
  4. )</code>

从集合中删除所有文档

  1. <code>> db.bios.find().count()
  2. 10
  3. > db.bios.remove( { } )
  4. WriteResult({ "nRemoved" : 10 })
  5. > db.bios.find().count()
  6. 0</code>

删除匹配条件的所有文档

  1. <code>> db.products.remove( { qty: { $gt: 20 } } )
  2. > db.products.find( { qty: { $gt: 20 } } ).count()
  3. 0</code>

使用参数justOne删除一条

  1. <code>> db.products.remove( { qty: { $gt: 20 } }, true )
  2. > db.products.find( { qty: { $gt: 20 } } ).count()
  3. 10</code>

MongoDB CRUD之D

标签:异常   iso   bio   多个   gpo   span   文档   oda   命令   

人气教程排行