Using the MGO multi-condition query in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Today by the MgO ferocious grammar toss on the ground for 1 hours to get out hereby commemorate:

Generally do a simple query, is written like this:

<pre name= "code" class= "plain" >collection: = Mgodbcontroller. Getmdb (). C (Mgodbcontroller. User_wallet_gs_log)//Get Action Object
According to the user's mobile phone number in reverse query First 100 deposit slice if err: = collection. Find (&bson. m{"Uphone": Phonenum}). Sort ("-initimesamp"). Limit (100). All (&historys); Err = = Nil {For I, _: = Range Historys {fmt. PRINTLN ("Data from MgO--->", historys[i]) ms = Append (MS, &historys[i])}} else {fmt. PRINTLN ("Query history", err) Logutils.getlog (). Error ("Query history", err)}

Now need to query these users, mobile phone number is 18112345678, while historical information is Reasona or REASONB history, if directly with the command to query, it is very simple, so write good:

Db.userwalletgslog.find ({"Uphone": "18112345678", "Reason": {"$in": ["Reasona", "Reasonb"]})
Next, the problem of bitterness, to use the Golang call MgO to achieve the above statement, the whole

Baidu N Long, find a so write: (link is http://blog.csdn.net/varding/article/details/17200299)

C.find (Bson. m{"name": Bson. m{"$in": []string{"Jimmy Kuu", "Tracy Yu"}}). All (&users)
His writing is actually a bit of a problem, at least my program is not run up, after a long ~~~~~~ time test, here 's the answer:

ERR: = collection. Find (&bson. m{"Uphone": "18112345678", "Reason": &bson. m{"$in": []string{"Reasona", "Reasonb"}}}). All (&historys)
See, see, this pit father's &bson. M I really mean it.





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.