Some strange behevier in Full Text Search command

25 views
Skip to first unread message

Kamoliddin Mavlonov

unread,
Mar 13, 2014, 10:02:04 AM3/13/14
to mgo-...@googlegroups.com
Hi guys,

The function bellow doesn't run stable.

func GetSearch(w http.ResponseWriter, r *http.Request) {
	session, err := mgo.Dial(conf.Mongodb)
	if err != nil {
		log.Fatal("Unable to connect to DB ", err)
	}
	defer session.Close()
	session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.

	db := session.DB("sa")
	var result interface{}
	// err = db.Run(bson.M{"text": "ad", "search": "hsjsjd"}, &result)
	err = db.Run(bson.M{"text": "ad", "search": "hsjsjd", "limit": 1}, &result)
	fmt.Printf("err = %s\n", err)

	if err != nil {
		w.Write(json.Message("ERROR", "Ads not found"))
	} else {
		w.Write(json.Message("OK", result))
	}
}

sometimes, it gives error:
err = no such cmd: limit
err = no such cmd: search

Is it something I am missing?

Regards,
Kamol

Kamoliddin Mavlonov

unread,
Mar 14, 2014, 10:54:25 AM3/14/14
to mgo-...@googlegroups.com
Ok, I have figured it out from Gustavo comments and Mgo doc.

func GetSearch(w http.ResponseWriter, r *http.Request) {
session, err := mgo.Dial(conf.Mongodb)
if err != nil {
log.Fatal("Unable to connect to DB ", err)
}
defer session.Close()
session.SetMode(mgo.Monotonic, true) // Optional. Switch the session to a monotonic behavior.
db := session.DB("sa")
var result interface{}
//db.ad.runCommand("text", { search: "hsjsjd", limit: 20, project: { "price" : 1, "image1": 1 }})
q := bson.D{
{"text", "ad"},
{"search", "hsjsjd"},
{"limit", 2},
{"project",
bson.D{
{"price", 1},
{"image1", 1},
},
},
}
err = db.Run(q, &result)
log.Printf("err = %s\n", err)

Gustavo Niemeyer

unread,
Mar 14, 2014, 11:03:03 AM3/14/14
to mgo-...@googlegroups.com
For the record, as discusssed in the other thread, the instability
comes from running the command with a map. MongoDB expects the command
name field to be the first one, and maps do not provide that
guarantee. Instead, use bson.D.
> --
> You received this message because you are subscribed to the Google Groups
> "mgo-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mgo-users+...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--

gustavo @ http://niemeyer.net
Reply all
Reply to author
Forward
0 new messages