我可以在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
最满意答案
我用
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
了测试kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big
vskCGImageAlphaPremultipliedFirst | 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
vskCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
and the profiler did show me that there is a slightly different code path.
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little
copies memory directly, whilekCGImageAlphaPremultipliedLast | 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 callsetNeedsDisplayInRect:
to show them on screen. Furthermore since the overall times are essentially identical, I'm sticking withkCGImageAlphaPremultipliedLast | 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.
-
iOS动画性能(iOS Animation Performance)[2021-02-16]
您可以使用本教程中描述的CoreAnimation 。 它很好地解释了您可以用来提高现在性能的所有技术(首先它不使用UIViews和标准animationImages ,其次它使用精灵(也称为纹理图册),这不仅仅是提高性能,但在管理图像资源方面也会让您的生活更轻松。 您还可以使用CADisplayLink创建一个game loop ,您可以在其中进行所有更新。 这里有几个问题/答案就是这样描述的。 You can use CoreAnimation as described in this tutorial ... -
iOS UIScrollView性能(iOS UIScrollView performance)[2023-12-31]
这里有几点建议: 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.
-
iOS GCD性能如何?(How about iOS GCD Performance?)[2022-07-12]
您在调用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设备上的性能问题(Performance issues on iOS device)[2023-10-20]
在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 ...
-
iOS UI性能分析(iOS UI performance profiling)[2024-02-03]
使用“仪器”工具“核心动画”来测量图形(以及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 ...