1. 查询语句:  MongoDB语法:

db.collection.find() 

Go BSON语法:

collection.Find() 

例子:查询users集合中所有年龄大于20岁的数据。

 MongoDB语法:db.users.find({age: {$gt: 20}}) 

Go BSON语法:collection.Find(bson.M{"age": bson.M{"$gt": 20}}) 

2. 插入语句:

 MongoDB语法:db.collection.insert() 

Go BSON语法:collection.InsertOne() 或 collection.InsertMany()

例子:在users集合中插入一个名为“Alice”的用户。

MongoDB语法:db.users.insert({name: "Alice"}) 

Go BSON语法:collection.InsertOne(bson.M{"name": "Alice"}) 

 3. 更新语句

MongoDB法:db.collection.update()

Go BSON语法:collection.UpdateOne() 或 collection.UpdateMany()

例子:将users集合中所有名字为“Bob”的用户的年龄改为30岁。 

MongoDB语法:db.users.update({name: "Bob"}, { $set: { age: 30 }}, {multi: true})

Go BSON语法:collection.UpdateMany(bson.M{"name": "Bob"}, bson.M{"$set": bson.M{"age": 30}})

4. 删除语句:                 

MongoDB语法:db.collection.remove()

Go BSON语法:collection.DeleteOne() 或 collection.DeleteMany()

例子:删除users集合中名字为“Charlie”的用户

MongoDB语法:db.users.remove({name: "Charlie"})

Go BSON语法:collection.DeleteOne(bson.M{"name": "Charlie"})

5. 分组语句:

MongoDB语法:db.collection.aggregate()

Go BSON语法:collection.Aggregate()

例子:在orders集合中,将所有订单按country字段分组,并统计每个组内的订单数量。

MongoDB语法:db.orders.aggregate([{$group: {_id: "$country", count: {$sum: 1}}}])

Go BSON语法:collection.Aggregate(bson.A{bson.M{"$group": bson.M{"_id": "$country", "count": bson.M{"$sum": 1}}}})

6. 排序语句:

MongoDB语法:db.collection.sort()

Go BSON语法:collection.Find().Sort()

 例子:查询products集合中,根据price字段降序排序。

MongoDB语法:db.products.find().sort({price: -1})

Go BSON语法:collection.Find().Sort("-price")

7. Count查询语句:

MongoDB语法:db.collection.count()

Go BSON语法:collection.CountDocuments()

例子:统计orders集合中订单状态为“shipped”的订单数量。

MongoDB语法:db.orders.count({status: "shipped"})

Go BSON语法:collection.CountDocuments(bson.M{"status": "shipped"})

8. 更新文档语句:

MongoDB语法:db.collection.updateOne()

Go BSON语法:collection.UpdateOne()

例子:将customers集合中名字为“John”且年龄为25岁的用户的地址更新为“123 Main St”

MongoDB语法:

db.customers.updateOne({name: "John", age: 25}, { $set: { address: "123 Main St" }})

Go BSON语法:

collection.UpdateOne(bson.M{"name": "John", "age": 25}, bson.M{"$set": bson.M{"address": "123 Main St"}})

MongoDB语法:db.collection.updateMany()

Go BSON语法:collection.UpdateMany()

MongoDB语法:db.customers.updateMany({name: "Bob"}, { $set: { address: "456 Elm St" }})

Go BSON语法:collection.UpdateMany(bson.M{"name": "Bob"}, bson.M{"$set": bson.M{"address": "456 Elm St"}})

9. 聚合语句:

MongoDB语法:db.collection.aggregate()

Go BSON语法:collection.Aggregate()

例子:在users集合中,根据年龄字段分组,并统计每个年龄段的用户数量

MongoDB语法:db.users.aggregate([{$group: {_id: "$age", count: {$sum: 1}}}])

Go BSON语法:collection.Aggregate(bson.A{bson.M{"$group": bson.M{"_id": "$age", "count": bson.M{"$sum": 1}}}})

10.模糊查询

MongoDB语法:db.products.find({name: /^apple/})

Go BSON语法:collection.Find(bson.M{"name": primitive.Regex{Pattern: "^apple", Options: ""}})

11.嵌套查询

MongoDB语法:db.users.find({hobbies: {$in: ["hiking"]}})

Go BSON语法:collection.Find(bson.M{"hobbies": bson.M{"$in": bson.A{"hiking"}}})

12.分页查询

MongoDB语法:db.collection.find().skip().limit()

Go BSON语法:collection.Find().Skip().Limit()

 例子:查询orders集合中,按order_date字段降序排序,跳过前20个订单,返回最多10个结果

MongoDB语法:db.orders.find().sort({order_date: -1}).skip(20).limit(10)

Go BSON语法:collection.Find().Sort("-order_date").Skip(20).Limit(10)

13.复合查询

例子:查询users集合中,年龄大于20岁且爱好包含“hiking”的用户

MongoDB语法:db.users.find({age: {$gt: 20}, hobbies: {$in: ["hiking"]}})

Go BSON语法:collection.Find(bson.M{"age": bson.M{"$gt": 20}, "hobbies": bson.M{"$in": bson.A{"hiking"}}})

14.游标查询

例子:查询orders集合中,按order_date字段降序排序,返回包含order_date和total字段的结果。

MongoDB语法:var cursor = db.orders.find().sort({order_date: -1})cursor.forEach(function(doc) { print(doc.order_date, doc.total); })

Go BSON语法:var cursor, err = collection.Find(context.Background(), bson.M{}, options.Find().SetSort(bson.M{"order_date": -1}))for cursor.Next(context.Background()) { var result bson.M err = cursor.Decode(&reslut) // 处理result中的order_date和total字段 }

好文链接

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: