首页 \ 问答 \ 我可以在MongoDB中的聚合查询中申请forEach吗?(Can I apply forEach in aggregate query in MongoDB?)

我可以在MongoDB中的聚合查询中申请forEach吗?(Can I apply forEach in aggregate query in MongoDB?)

我有一个成员集合,并按特定条件查找成员,在获得成员后,我需要为每个成员进行一些计算。 计算需要查询同一个集合。

我的过程是

var eachMemberInfo = [];
var members = db.collection('member').find({ createdDate: currentDate, country: 'BD'}).toArray();
members.forEach(function(doc) {
  var result = db.collection('member').aggregate([
    { $match: { memberType: doc.memberType, country : doc.country } },
    {
      $group: {
        _id: {memberType:"$memberType",country:"$country"},
        memberCount: { $sum: {$cond:[{$gt: ["$numberOfInvitees",0]},1,0]} },
        lessCount: { $sum: {$cond:[{$and:[{$lt:["$numberOfInvitees", doc.numberOfInvitees]}, {$gt: ["$numberOfInvitees",0]}]},1,0]} },
        sameCount: { $sum: {$cond:[{$eq: ["$numberOfInvitees",doc.numberOfInvitees]},1,0]} }
      }
    }
  ]).toArray();

   eachMemberInfo.push({memberId:doc.memberId,memberCount: result[0].memberCount, lessCount: result[0].lessCount});

});

我的问题是如何使用单个聚合查询来执行此操作?

任何人都可以帮助我:)

例如:

会员集合如:

[{
    "_id" : ObjectId("57905b2ca644ec06142a8c06"),
    "memberID" : 80,
    "memberType" : "N",
    "numberOfInvitees" : 2,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
},
{
    "_id" : ObjectId("57905b2ca644ec06142a8c09"),
    "memberID" : 81,
    "memberType" : "N",
    "numberOfInvitees" : 3,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8fgh"),
    "memberID" : 82,
    "memberType" : "N",
    "numberOfInvitees" : 4,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
    "memberID" : 83,
    "memberType" : "N",
    "numberOfInvitees" : 1,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
    "memberID" : 84,
    "memberType" : "N",
    "numberOfInvitees" : 2,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
..............
]

eachMemberInfo中的预期结果如:

[
  { memberID : 80, memberCount:5,lessCount: 1,sameCount:2 },
  { memberID : 81, memberCount:5,lessCount: 3,sameCount:1 },
  { memberID : 82, memberCount:5,lessCount: 4,sameCount:1 },
  { memberID : 83, memberCount:5,lessCount: 0,sameCount:1 },
  { memberID : 84, memberCount:5,lessCount: 1,sameCount:2 }
]

I have a member collection and find members by specific conditions and after get members I need to do some calculation for each member. To calculate need query on the same collection.

My process is

var eachMemberInfo = [];
var members = db.collection('member').find({ createdDate: currentDate, country: 'BD'}).toArray();
members.forEach(function(doc) {
  var result = db.collection('member').aggregate([
    { $match: { memberType: doc.memberType, country : doc.country } },
    {
      $group: {
        _id: {memberType:"$memberType",country:"$country"},
        memberCount: { $sum: {$cond:[{$gt: ["$numberOfInvitees",0]},1,0]} },
        lessCount: { $sum: {$cond:[{$and:[{$lt:["$numberOfInvitees", doc.numberOfInvitees]}, {$gt: ["$numberOfInvitees",0]}]},1,0]} },
        sameCount: { $sum: {$cond:[{$eq: ["$numberOfInvitees",doc.numberOfInvitees]},1,0]} }
      }
    }
  ]).toArray();

   eachMemberInfo.push({memberId:doc.memberId,memberCount: result[0].memberCount, lessCount: result[0].lessCount});

});

My question is how can I do this using single aggregate query?

can any one help me pls :)

For example:

member collection like:

[{
    "_id" : ObjectId("57905b2ca644ec06142a8c06"),
    "memberID" : 80,
    "memberType" : "N",
    "numberOfInvitees" : 2,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
},
{
    "_id" : ObjectId("57905b2ca644ec06142a8c09"),
    "memberID" : 81,
    "memberType" : "N",
    "numberOfInvitees" : 3,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8fgh"),
    "memberID" : 82,
    "memberType" : "N",
    "numberOfInvitees" : 4,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
    "memberID" : 83,
    "memberType" : "N",
    "numberOfInvitees" : 1,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
{
    "_id" : ObjectId("57905b2ca644ec06142a8cfgd"),
    "memberID" : 84,
    "memberType" : "N",
    "numberOfInvitees" : 2,
    "createdDate" : ISODate("2016-07-21T00:00:00.000Z"),
    "country" : "BD"
}
..............
]

Expected result in eachMemberInfo like:

[
  { memberID : 80, memberCount:5,lessCount: 1,sameCount:2 },
  { memberID : 81, memberCount:5,lessCount: 3,sameCount:1 },
  { memberID : 82, memberCount:5,lessCount: 4,sameCount:1 },
  { memberID : 83, memberCount:5,lessCount: 0,sameCount:1 },
  { memberID : 84, memberCount:5,lessCount: 1,sameCount:2 }
]

原文:https://stackoverflow.com/questions/38496419
更新时间:2022-07-30 17:07

最满意答案

我用kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big了测试 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big vs kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little和分析器确实告诉我代码路径略有不同。

kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little直接复制内存,而kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big通过转换函数。

这对我在CGBitmapContext绘制路径所花费的总时间(甚至在iPod 4上)几乎没有影响,然后调用setNeedsDisplayInRect:在屏幕上显示它们。 此外,由于总时间基本相同,我坚持使用kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big

编辑我切换回kCGBitmapByteOrder32Little ,以便与使用BGRA(小端)字节顺序的其他iOS框架(如AVFoundation)具有更好的兼容性,因为它对性能没有影响。


I ran tests with kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big vs kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little and the profiler did show me that there is a slightly different code path.

kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little copies memory directly, while kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big goes through a conversion function.

This made almost no impact in the total time it took to draw paths in my CGBitmapContext (even on an iPod 4) and then call setNeedsDisplayInRect: to show them on screen. Furthermore since the overall times are essentially identical, I'm sticking with kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big.

EDIT I switched back to kCGBitmapByteOrder32Little in order to have better compatibility with other iOS frameworks (like AVFoundation) that use BGRA (little endian) byte orders since there was no impact on performance.

相关问答

更多
  • 是的 - 您在应用上运行仪器的时间档案,告诉您到底花了多少时间,以及在哪里 。 它会告诉你它是图像,字符串,阴影还是其他东西。 Yes - you run Instruments' Time Profile on your app to tell you exactly how much time is spent, and where. It will tell you if it is the image, string, shadow, or something else.
  • 您可以使用本教程中描述的CoreAnimation 。 它很好地解释了您可以用来提高现在性能的所有技术(首先它不使用UIViews和标准animationImages ,其次它使用精灵(也称为纹理图册),这不仅仅是提高性能,但在管理图像资源方面也会让您的生活更轻松。 您还可以使用CADisplayLink创建一个game loop ,您可以在其中进行所有更新。 这里有几个问题/答案就是这样描述的。 You can use CoreAnimation as described in this tutorial ...
  • 这里有几点建议: 1)首先,了解scrollViewDidScroll:将在用户滚动时连续调用。 不只是每页一次。 所以,我会确保你有逻辑确保加载中涉及的实际工作仅在每页触发一次 。 通常,我会保留一个像int lastPage这样的类。 然后,当scrollViewDidScroll:被调用时,我计算新的当前页面 。 只有当它与伊娃不同时才会触发装载。 当然,那么你需要将动态计算的索引(代码中的currentPage )保存在你的ivar中。 2)另一件事是我尽量不要在scrollViewDidScrol ...
  • 您可以使用Instrument的核心动画工具来分析您的应用程序。 它会给你FPS的读数。 请注意,这只适用于设备,不适用于模拟器。 You can profile your app with Instrument's Core Animation tool. It will give you the FPS reading. Note that this is only available on device and not on simulator.
  • 您在调用dispatch_queue_create时分配新的调度队列。 您必须使用Apple提到的dispatch_release将其释放。 否则你的内存就会耗尽! https://developer.apple.com/reference/dispatch/1453030-dispatch_queue_create#return-value 您可以通过dispatch_queue_create方法的返回值获取对新创建的队列的引用,然后将其释放。 以下是Apple在其文档中提到的内容。 当您的应用程序不再需 ...
  • iOS对于刷新到磁盘的频率非常聪明。 我注意到,当我背景的应用程序,只要我调用msync ,即msync(self.memoryMap, self.memoryMapLength, MS_SYNC); 它正确冲洗。 在使用应用程序时,即使发生崩溃或突然终止,通常所有数据都会保存。 如果我在调试过程中杀死了我的应用程序,有时候最后几项更改没有保存,但通常都会保存。 所以我的结论是,这不是一个问题。 iOS不会不断写入磁盘,而是以智能间隔写入磁盘。 iOS is pretty smart about how o ...
  • 我用kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big了测试 kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big vs kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little和分析器确实告诉我代码路径 ...
  • 在iOS上使用JavaFXPorts进行部署时的默认体系结构是thumbv7 ,如此处所示 。 这意味着应用程序以32位模式创建。 自iOS 10.1起,Apple正试图推动开发人员将其应用程序更新为64位模式 。 虽然创建32位应用程序并不意味着它们具有较低的性能,但如果您想要避免在64位模式下创建应用程序所需的消息。 为此,您可以更改arch参数: 命令行: ./gradlew -Probovm.arch=arm64 launchIOSDevice 或者在build.gradle文件中: jfxmob ...
  • 你的大多数问题的答案都是“它取决于”。 在实际目标设备上进行测试并查看它是否可接受是无可替代的。 我有两个一般建议:(a)确保你拥有尽可能少的纹理/精灵表,理想情况下每个场景一个2D游戏。 并且(b)是的,我会为不同的目标设备创建重新调整大小/重新采样的图形,而不是依赖于动态缩放。 这对于像iPad mini这样的非Retina硬件来说非常重要,因为不必要地加载巨大的图像是一个糟糕的主意。 最好先预先下载一点,然后再进行最佳的游戏体验。 The answer to most of your question ...
  • 使用“仪器”工具“核心动画”来测量图形(以及UI)性能。 主要以帧率(这是测量平滑度的正式方式)的形式,但您也可以将其配置为显示重叠和混合视图(您的GPU绝对不喜欢)。 此外,还有一些很棒的WWDC会议可供iOS开发人员使用。 Use the Instruments tool 'Core Animation' to measure graphics (and thus UI) performance. Mostly in the form of frame rate (which is a formal w ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。