Tag: mongoose es6 promise

使用mongoose 4.11.0进行组查询

我想用最后一个版本的mongoose做一个例子: Patient.group({gender:true}) .then((result)=>{ console.log(result); }); 病人是模型,你知道这是怎么回事?

Mongoose:改进find()和count()查询

背景 我在Mongoose中有一个查询,find一组对象,然后将这些对象与它们的总数一起返回: var itemsPerPage = 4, pageNum = 1; Survey.find({}) .limit(itemsPerPage) .skip(itemsPerPage * pageNum) .then(docs => { if (docs.length === 0) console.log("There are no results matching your query."); else DocModel.count({}) .then(number => { console.log(JSON.stringify({number, docs})); }); }) .catch(console.log); 要做到这一点,我做到了: 用参数find查询 使用limit和skip从正确的页面获得结果 如果docs不为空,请启动count查询 返回信息 问题 这里的问题,因为我需要在查询分页,是我必须做2个单独的请求数据库( find和count ),这可能会非常沉重。 为了优化这个,我想避免count查询,但我不知道如何。 另一个问题是嵌套承诺反模式,但这不是我现在要关注的问题。 题 我很乐意提供任何有关改进此代码的build议!

mongoose承诺不工作

我的mongoosefunction忽略承诺查询,并立即落下,而不用等待另一个步骤 exports.editProduct = (id, data) => { Product.findById(id) .then((data) => { var imgS3arr = data.img; var tempArr = []; var i = imgS3arr.length while (i–) { if (!imgS3arr[i].match(/https:\/\/s3.eu-central-1.amazonaws.com\/es-shop\//g)) { tempArr.push(imgS3arr[i]) imgS3arr.splice(i, 1) } } return tempArr }) .then((tempArr) => { var tempArrS3 = [] return Promise.all(tempArr.map((img, i) => { return fetch.remote(img).then((base) => { var buf […]