如何看懂mongoose,以deleteMany为例

Query.prototype.deleteMany()

Parameters
  • [filter] «Object|Query» mongodb selector
  • [options] «Object» optional see Query.prototype.setOptions()
  • [callback] «Function» optional params are (error, mongooseDeleteResult)

这里callback后面的两个参数表示function后也应接两个参数,例如err和raw

Example
    userRegister.deleteMany({
        registerTime: {$regex : '2021-3-'},
        userClass: 'visitor'
    }, function (err, raw) {
        if (err) console.log('userRegister.find failure' + err)
        else console.log('删除访客数:' + raw.deletedCount)
    })
Returns:
  • «Query» this

Declare and/or execute this query as a deleteMany() operation. Works like remove, except it deletes every document that matches filter in the collection, regardless of the value of single.

This function triggers deleteMany middleware.

Example

await Character.deleteMany({ name: /Stark/, age: { $gte: 18 } });

// Using callbacks:
Character.deleteMany({ name: /Stark/, age: { $gte: 18 } }, callback);

This function calls the MongoDB driver's Collection#deleteMany() function. The returned promise resolves to an object that contains 3 properties:

  • ok: 1 if no errors occurred
  • deletedCount: the number of documents deleted
  • n: the number of documents deleted. Equal to deletedCount.

这里表示所返回的参数raw的属性,比如删除数据的条数不能再用raw.length了,而应该用raw.deletedCount。

你可能感兴趣的:(如何看懂mongoose,以deleteMany为例)