Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

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")}]})
`

最终想要的效果是这样的
image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

正确的写法如下

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 }

效果图
image.png

插入一组数据

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")}]});

image.png

查询文档

db.getCollection('runoob').find({"orderLines": {"$exists" : true}}).pretty()

插入之后的结果如下图所示
image.png

image.png


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...