NoSQL泛指非关系型数据库,如mongo,redis,HBase。mongo使用高效的二进制数据存储,文件存储格式为 BSON ( 一种json的扩展,比json性能更好,功能更强大)。MySQL中表的概念在mongo里叫集合(collection), MySQL中行的概念在mongo中叫文档(document),一个文档看上去像一个json。
1.MongoDB与图形化窗口下载
下载地址:MongoDB Community Download | MongoDB
解压到你想解压的地方后创建两个文件夹(数据目录和日志目录) ?
管理员模式cmd进入bin目录下,
mongodb --install --dbpath data目录路径 --logpath log目录路径\mongodb.log
按本人目录的实际代码?
mongod --install --dbpath D:\mongodb-win32-x86_64-windows-5.0.13\data --logpath D:\mongodb-win32-x86_64-windows-5.0.13\logs\mongodb.log
?启动服务
net start mongodb
进入该网址,显示如下则成功?

?图形化窗口下载链接:MongoDB Compass Download | MongoDB
?登录后可进行终端操作
use test; 切换到test库,如果没有则(创建集合时)会自动创建
db.createCollection("student"); 创建collection
db.createUser({user: "tester",pwd: "123456", roles: [{role: "dbAdmin", db: "test"}]});创建用户
db.student.createIndex({"name":1});在name上创建索引,不是唯一索引
db.student.insertOne({name:"张三",city:"北京"});
db.student.find({name:"张三"});
db.student.updateOne({name:"张三"},{$set:{name:"张三",city:"上海"}})
db.student.deleteOne({name:"张三"});
切换用户
2.GO操作MongoDB
?安装go MongoDB driver
go get go.mongodb.org/mongo-driver
go get go.mongodb.org/mongo-driver/x/bsonx/bsoncore@v1.7.1
go get go.mongodb.org/mongo-driver/x/mongo/driver@v1.7.1
go get go.mongodb.org/mongo-driver/mongo/options@v1.7.1
go get go.mongodb.org/mongo-driver/x/mongo/driver/topology@v1.7.1
go get go.mongodb.org/mongo-driver/mongo@v1.7.1
连接DB:
option := options.Client().ApplyURI("mongodb://127.0.0.1:27017").SetConnectTimeout(2 *time.Second).SetAuth(options.Credential{Username:"tester",Password:"123",AuthSource: "test"})
client, err := mongo.Connect(context.Background(),option)
CheckError(err)
err = client.Ping(context.Background(),nil)//ping通才代表连接成功
CheckError(err)
collection := client.Database("test").Collection("student")//选择对应集合
创建文档:
func create(ctx context.Context,collection *mongo.Collection){
doc := Student{Name: "刘刘",Age: 18,Score: 90}
res, err := collection.InsertOne(ctx,doc)
CheckError(err)
fmt.Printf("insert id %v \n", res.InsertedID)
docs := []interface{}{Student{Name:"李四",Age:15,Score:50},Food{Sweet: 70.1,Spices: 0.0, Salty: 10.3}}
ress,err := collection.InsertMany(ctx,docs)
fmt.Printf("insert many ids:%v\n", ress.InsertedIDs)
}
更新文档:
func update(ctx context.Context, collection *mongo.Collection){
filter := bson.D{{"city","北京"}}
update := bson.D{{"$inc",bson.D{{"score",5}}}}//inc为increase
res, err := collection.UpdateMany(ctx,filter,update)
CheckError(err)
fmt.Printf("update %d doc \n",res.ModifiedCount)
}
查询文档:
func query(ctx context.Context, collection *mongo.Collection){
sort := bson.D{{"name", 1}}//1为升序
filter := bson.D{{"score", bson.D{{"$gt",3}}}}
findOption := options.Find()
findOption.SetSort(sort)
findOption.SetLimit(10)
findOption.SetSkip(1)
cursor, err := collection.Find(ctx, filter, findOption)
CheckError(err)
for cursor.Next(ctx){
var doc Student
err := cursor.Decode(&doc)
CheckError(err)
fmt.Printf("%s %d %d\n", doc.Name, doc.Age, doc.Score)
}
}
删除文档:
func delete(ctx context.Context, collection *mongo.Collection){
filter := bson.D{{"name","张三"}}
res, err := collection.DeleteMany(ctx, filter)
CheckError(err)
fmt.Printf("delete %d doc \n",res.DeletedCount)
}
|