mgo-Follow-up test (specify field, get ID)

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

After testing the MgO in the Dbref, want to test the display of the specified field, only to find that the original frame coding, a lot of problems are hidden up:

1. Display the specified field:

Previously, when using MgO, all fields were queried, and the MONGO terminal environment was written in the following format:

> Db.logs.Find({},{"Log":1}){ "_id":"3a06384a40a8e587806f194c6c80253e","Log":"This is a test log" }{ "_id":"36BB718040A4362B8035EBD822125DBA","Log":"This is a test log"}

To ensure that only the log content is displayed, you need to remove the ID display, write this

> Db.logs. Find ({},{"_id":0,"log":1"  Log "" This is a test log ""  "" This is a test log " }

The MgO notation in Golang needs to be implemented with the Select () method in MgO, with the following code:

Err = D.find (Bson. m{}). Select (Bson. m{"log"1}). All (&RESULT1)

The results are as follows:

 Done 342  0001-------- xx: +0000   0001------XX: +0000 UTC}

Remove the ID display:

Err = D.find (Bson. m{}). Select (Bson. m{"Log": 1, "_id": 0}). All (&RESULT1)

Results

0001-------XX : +00000001- 0000 -xx: +/- UTC}

Well..... If you want to display only one field, the way I do it now is to loop the output.

For I: = 0; I < Len (RESULT1); i++ {fmt. Println (Result1[i]. LOG)}

Reference:How does MgO return part of field?

2. The value of the query ID:

The MONGO terminal is simple:

> Db.logs. Find ({},{"_id":1"_id""  3a06384a40a8e587806f194c6c80253e ""_id""  36bb718040a4362b8035ebd822125dba" }

You can't find it under MgO? The problem is in the struct

Error notation type Log struct {logid    stringlog      stringloguser  MgO. dbrefinserted time. time}//correct notation type Log struct {logid    string ' Bson: "_id" ' Log      stringloguser  MgO. dbrefinserted time. TIME}

Re-Query ID no problem:), pay attention to check the time is "_id" and not Logid

Err = D.find (Bson. m{}). Select (Bson. m{"_id": 1}). All (&RESULT1)

Results:

{32197b67400e229f8017fd8258c2f700  0001-------- xx: +0000 
{E7DDA7B34052584280EF55F49B16EB2C 0001------XX: +0000 UTC}

Reference:How does MgO query the result according to the specified _id?

  

Related Article

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.