MongoDB 插入数据使用NumberDecimal报错
下面这两种在mongodb中插入数据都会报错
第一种:db.orders.insertOne({ "street": "493 Milll Curve", "city": "Chongqing", "state": "Texas", "country": "Malaysia", "zip": "24344-1715", "phone": "19734347645", "name": "Ellia", "userId": 3573, "orderDate": new Date(), "status": "created", "shippingFee": mongodb.Decimal128.fromString("8.00"), "orderLines": [{ "product1": "Refined Fresh Tuna", "sku": "2097", "qty": 24, "price": mongodb.Decimal128.fromString("56.00"), "cost": mongodb.Decimal128.fromString("46.45")}, { "product2": "Refined Fresh Tuna", "sku": "2000", "qty": 32, "price": mongodb.Decimal128.fromString("56.00"), "cost": mongodb.Decimal128.fromString("87")}]})
第二种:
`db.orders.insertOne({ "street": "493 Milll Curve", "city": "Chongqing", "state": "Texas", "country": "Malaysia", "zip": "24344-1715", "phone": "19734347645", "name": "Ellia", "userId": 3573, "orderDate": new Date(), "status": "created", "shippingFee": NumberDecimal("8.00"), "orderLines": [{ "product1": "Refined Fresh Tuna", "sku": "2097", "qty": 24, "price": NumberDecimal("56.00"), "cost": NumberDecimal("46.45")}, { "product2": "Refined Fresh Tuna", "sku": "2000", "qty": 32, "price": NumberDecimal("56.00"), "cost": NumberDecimal("87")}]})
`
最终想要的效果是这样的
回答:
正确的写法如下
db.runoob.insertMany([{"title": "MongoDB Overview","description" : "MongoDB is no sql database", "by_user": "runoob.com", "url" : "http://www.runoob.com", "tags" : ['mongodb', 'database', 'NoSQL'], "likes" : 100},
{"title": "NoSQL Overview","description" : "No sql database is very fast", "by_user": "runoob.com", "url" : "http://www.runoob.com", "tags" : ['mongodb', 'database', 'NoSQL'], "likes" : 10},
{"title": "Neo4j Overview","description" : "Neo4j is no sql database", "by_user": "Neo4j.com", "url" : "Neo4j is no sql database", "tags" : ['neo4j', 'database', 'NoSQL'], "likes" : 750}
]);
db.runoob.aggregate([{$group: {_id: "$by_user", num_tutorial : {$sum : 1}}}])
{ "_id" : "Neo4j.com", "num_tutorial" : 1 }
{ "_id" : "runoob.com", "num_tutorial" : 2 }
效果图
插入一组数据
db.runoob.insertOne({"street": "493 Milll Curve","city" : "Chongqing", "country": "Malaysia", "zip" : "24344-1715", "phone" : "19734347645", "name" : "Ellia", "userId" : 2467, "orderDate": new Date(), "status" : "created", "shippingFee" : NumberDecimal("8.00"), "orderLines": [{ "product1": "Refined Fresh Tuna", "sku": "2097", "qty": 24, "price": NumberDecimal("56.00"), "cost": NumberDecimal("46.45")}, { "product2": "Refined Fresh Tuna", "sku": "2000", "qty": 32, "price": NumberDecimal("56.00"), "cost": NumberDecimal("87")}]});
查询文档
db.getCollection('runoob').find({"orderLines": {"$exists" : true}}).pretty()
插入之后的结果如下图所示
以上是 MongoDB 插入数据使用NumberDecimal报错 的全部内容, 来源链接: utcz.com/a/157832.html