【发布时间】:2020-05-29 19:37:43
【问题描述】:
我在 MongoDB 的收藏中有这个,所以我只需要在 MED0 中计算逗号并返回 4。
当我在 Firebase 中执行此操作时,是这样的:
let medRef = db.ref('root/users/id1/MED0');
// change the object in an element of array
function snapshotToArray(snapshot) {
let returnArr = [];
snapshot.forEach(function(childSnapshot) {
let item = childSnapshot.val();
item.key = childSnapshot.key;
str= item.split(/\d+/).length;//split divide a string in an array of strings, and I use /\d+/
//lenght counts the characters
returnArr.push(str - 2); //-2 no count []
});
return returnArr;
}; // on() synchronize data and snap is a snapshot of database
medRef.on('value', function(snapshot) {
console.log(snapshotToArray(snapshot));
});
那么我如何在 Mongoose 中做同样的事情并在 MongoDB 中返回我的集合中的值?
【问题讨论】:
-
啊,我错过了您问题的最后一行,因为它已与代码合并。请修正您的问题的格式以使其更清晰。
标签: mongodb firebase mongoose mongodb-query aggregate-functions