Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Mongodb query case, golang Library mgo case

2024-05-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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. "}]}

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report