Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongo-go-driver find a document by _id

Tags:

mongodb

go

I'm trying to find a document by its auto generated _id field. Code below returns nothing:

var documentID bson.RawValue
documentID.Type = 7
documentID.Value = []byte("5c7452c7aeb4c97e0cdb75bf")
objID := documentID.ObjectID()
value := collection.FindOne(ctx, bson.M{"_id": objID})

The value I provided is a real document id I got from Mongo Express

"_id": ObjectID("5c7452c7aeb4c97e0cdb75bf")

In case you're wondering why I bother with RawValue, I found examples using bson.EC.ObjectID but bson package doesn't seem to have EC type, also I found some examples mentioning github.com/mongodb/mongo-go-driver/bson/objectid package, but I could not find that package either. I previously developed with mgo but I'm new to mongo-go-driver, so if you can point an easy way to declare an ObjectID.

like image 745
Serdar Kalaycı Avatar asked Mar 02 '19 21:03

Serdar Kalaycı


People also ask

How do I find one record in MongoDB?

MongoDB – FindOne() Method. The findOne() method finds and returns one document that matches the given selection criteria. If multiple documents satisfy the given query expression, then this method will return the first document according to the natural order which reflects the order of documents on the disk.

How do I get documents from MongoDB?

In MongoDB, find() method is used to select documents in a collection and return a cursor to the selected documents. Cursor means a pointer that points to a document, when we use find() method it returns a pointer on the selected documents and returns one by one.

How do I search multiple documents in MongoDB?

You can query for multiple documents in a collection with collection. find() . The find() method uses a query document that you provide to match the subset of the documents in the collection that match the query.

How does the value of _ID get assigned to a document MongoDB?

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field. This also applies to documents inserted through update operations with upsert: true.


2 Answers

As @Carlos mentioned, I changed my code as this and everything works well.

objID, _ := primitive.ObjectIDFromHex("5c7452c7aeb4c97e0cdb75bf")
value := collection.FindOne(ctx, bson.M{"_id": objID})
like image 192
Serdar Kalaycı Avatar answered Oct 06 '22 06:10

Serdar Kalaycı


package main

import (
    "fmt"
    "go.mongodb.org/mongo-driver/bson"
    "go.mongodb.org/mongo-driver/bson/primitive"
    "context"
)





// get collection "users" from db() which returns *mongo.Client
var userCollection = db().Database("goTest").Collection("users") 


func mongodriver_find_by_id() {
    
    
    objectId, err1 := primitive.ObjectIDFromHex("6041c3a6cfcba2fb9c4a4fd2")
    if err1 != nil {fmt.Println(err1)}


    findone_result := userCollection.FindOne(context.TODO(), bson.M{"_id":objectId})
    var bson_obj bson.M
    if err2 := findone_result.Decode(&bson_obj); err2 != nil {fmt.Println(err2)}
    fmt.Println("bson_obj:", bson_obj)


}
like image 20
quine9997 Avatar answered Oct 06 '22 05:10

quine9997