使用1衬里通过mongo shell通过mongodb中的$ inc来更新许多记录,我发现我触摸的整数字段转换为双精度型。我知道Shell使用javascript,这就是原因。我的问题:是否可以通过以某种方式传入NumberInt来继续使用1-liner进行此更新,还是我需要其他东西? 鉴于:
{ _id:1 sku:“ abc123”, 数量:10, 指标:{ 订单:2 评分:3.5 } }
然后我尝试1-liner。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<phase>package</phase>
<excludes>
<exclude>**/*.java</exclude>
<exclude>**/MyClass.class</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
<finalName>jar-with-dependencies</finalName>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>MyClass</mainClass>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.handlers</resource>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>META-INF/spring.schemas</resource>
</transformer>
<transformer>
implementation="org.apache.maven.plugins.shade.resource.AppendingTransformer">
<resource>MyClass.class</resource>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
数量应为8而不是8.0,指标应为3而不是3.0。
答案 0 :(得分:0)
要保留原始整数数据类型,您需要将$inc
值传递为NumberInt
:
db.products.update(
{ sku: "abc123" },
{ $inc: { quantity: NumberInt(-2), "metrics.orders": NumberInt(1) } }
)