Get the App
SLTechnology News&Howtos  ›  Database  › 

Mongodb query case, golang Library mgo case

Shulou Source: shulou.com Published: 2022-06-01 22:45:01 11月06日 Update

Pipeline: = [] bson.M {bson.M {"$match": bson.M {"id": uid}}, bson.M {"$project": bson.M {"last_msg": 1, "_ id": 0}}, bson.M {"$unwind": "$last_msg"}, bson.M {"$sort": bson.M {"last_msg":-1}}, bson.M {"$skip": 2}, bson.M {"$limit": 2} } userModel: = MongoDb.C ("user") pipe: = userModel.Pipe (pipeline) var data [] interface {} err: = pipe.All (& data) fmt.Println (data) fmt.Println (len (data))

Equivalent to (query id is 1, only last_msg fields are taken out, no _ id is taken, and the array of last_msg is extracted and expanded, arranged in reverse order by last_msg.date, the first two items are skipped, and only two items are selected):

Db.getCollection ("user") .aggregate ([{$match: {"id": "1"}, {$project: {"last_msg": 1, "_ id": 0}}, {$unwind: "$last_msg"}, {$sort: {"last_msg.date":-1}}, {$skip:2}, {$limit:2}])

Mongodb structure:

One of the data:

{"_ id": ObjectId ("5d2b24c16197934ef6db77ba"), "id": "1", "account": "your goddess", "passwd": "5d78eb174d633345054faf7d56a612ed", "friends": [{"uid": "2"}, {"uid": "3"}] "last_msg": [{"date": "1234567890", "from_id": "2", "msg": "here"}, {"date": "1234567895", "from_id": "3", "msg": "Goddess"} {"date": "1234567898", "from_id": "4", "msg": "Please Straighten me out "}, {" date ":" 1234567999 "," from_id ":" 5 "," msg ":" are you with someone else. "}]}

Tags: Goddess case query field data array structure reverse order and set Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Docker Apple OPPO Reno NVidia