Your organization has decided to remove all emails from the last century stored in the emails collection. The timestamp for emails in your collection is stored in a field called date. Which of the following queries will achieve this result? Choose the best answer:
题目选项
- A.db.emails.deleteOne( { "date": { "$before": new Date( "2000-01-01" ) } } )
- B.db.emails.deleteMany( { "date": { "$lt": new Date( "2000-01-01" ) } } )
- C.db.emails.deleteMany( { "date": new Date( "2000-01-01" ) } )
- D.db.emails.delete( { "date": { "$lt": new Date( "2000-01-01" ) } } )
- E.db.emails.drop( { "date": { "$lt": new Date( "2000-01-01" ) } } )
答案置信度:高
正确答案:B
解析语言:中文
要删除多条文档必须用 deleteMany,时间条件用 $lt new Date("2000-01-01") 表示 2000 年之前的"上个世纪"邮件,故 B 正确。A 的 $before 不是 MongoDB 操作符;C 是日期等值匹配;D 的 db.emails.delete() 不是集合的有效方法;E 的 drop 会删除整个集合且不接受查询条件。