正确的写法如下
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()
插入之后的结果如下图所示