Skip to content

Differences between Qmgo and Mgo

jiangz222 edited this page Dec 1, 2020 · 7 revisions

Configuration

About initial configuration, qmgo will refer to some of the official mongoDB driver, such as using ReadPref instead of Mgo's Mode

	cfg = Config{
		Uri:              "mongodb://localhost:27017",
		Database:         "user",
		Coll:             "classs",
		ReadPreference:   &ReadPref{Mode: readpref.SecondaryMode, MaxStalenessMS: 500},
	}
	cli, err := Open(context.Background(), &cfg)

API

  • Most API need Context as parameter

  • The implement of NewObjectID() is different,ObjectId is string in mgo and [12]byte in qmgo

  • Update interfaces(UpdateOne、UpdateAll...) must contain key beginning with '$': Following codes are illegal in qmgo or official mongoDB driver, which is legal in mgo

	filter := bson.M{
		"name": "Alice",
	}
	update := bson.M{
	//	"$set": bson.M{    // this is required in qmgo or official mongoDB driver, not required in mgo
			"name": "Alice1",
			"age":  18,
	//	},
	}
	err = cli.UpdateOne(context.Background(), filter, update)
  • The name of interface
    Update(...) -> UpdateOne(...)
    Insert(...) -> InsertOne(...)

Data is different in MongoDB

Clone this wiki locally