MongoDB聚合框架$ subtract

编程入门 行业动态 更新时间:2024-10-25 00:26:55
本文介绍了MongoDB聚合框架$ subtract的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用mongodb来实现简单的查询,例如mysql从表中选择a-b",但是聚合框架的查询结果不正确.

I'm want use mongodb to achieve simple query like mysql "select a-b from table", but aggregation framework query result is not right.

数据:

{ "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 1, "b" : 1, "name" : "xxxxx0" } { "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 2, "b" : 2, "name" : "xxxxx1" }

mongodb cmd:

mongodb cmd:

db.site.aggregate([ { $match: { "a" : {$exists:true}, "b" : {$exists:true}, } }, { $project: { _id : 0,name : 1, r1: {$subtract:["$a", "$b"]} } }, { $limit: 100 }, ]); "result" : [ { "name" : "xxxx1", "r1" : -1 }, { "name" : "xxxx0", "r1" : -2 }, ]

推荐答案

我无法复制您的行为:

> db.tg.find() { "_id" : ObjectId("511223348a88785127a0d13f"), "a" : 1, "b" : 1, "name" : "xxxxx0" } > db.tg.aggregate([{ $match: { "a" : {$exists:true}, "b" : {$exists:true} } }, { $project: { _id : 0,name : 1, r1: {$subtract:["$a", "$b"]} }}, { $limit: 100 }]) { "result" : [ { "name" : "xxxxx0", "r1" : 0 } ], "ok" : 1 }

您能给我们更多信息,例如您的MongoDB版本吗?

Can you give us a little more info like your MongoDB version?

更多推荐

MongoDB聚合框架$ subtract

本文发布于:2023-11-22 18:24:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1618514.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   MongoDB   subtract

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!