MongoDB合并值等于给定的目标值
假设:
db.inventory.insertMany( [
{ "_id" : 1, "item" : "abc123", "price" : 2 },
{ "_id" : 2, "item" : "xyz123", "price" : 5 },
{ "_id" : 3, "item" : "ijk123", "price" : 12},
{ "_id" : 4, "item" : "fgh123", "price" : 14},
] )
用户有17美元可花费,我需要为该预算返回产品组合。它应该返回:
{ "_id" : 2, "item" : "xyz123", "price" : 5 },
{ "_id" : 3, "item" : "ijk123", "price" : 12},
这可能吗?目前,我要拉所有文档,然后运行递归功能。但是它使用了很多内存。
Let's assume:
db.inventory.insertMany( [
{ "_id" : 1, "item" : "abc123", "price" : 2 },
{ "_id" : 2, "item" : "xyz123", "price" : 5 },
{ "_id" : 3, "item" : "ijk123", "price" : 12},
{ "_id" : 4, "item" : "fgh123", "price" : 14},
] )
A user has $17 dollars to spend, I need to return a combination of products for that budget. It should return:
{ "_id" : 2, "item" : "xyz123", "price" : 5 },
{ "_id" : 3, "item" : "ijk123", "price" : 12},
Is this possible? Currently, I'm pulling all the docs, then running a recursive function. But it uses a lot of memory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论