How to make a composite query?

475 views
Skip to first unread message

Alex Luya

unread,
Aug 5, 2012, 6:03:28 AM8/5/12
to mgo-...@googlegroups.com
My data structure like this:
   Blog:
         id                       string
	 title                    string
         content               string
              comments:
                           id                string
                           content        string

And if I want to get a comment with id:000000000000 in blog with id:1111111111111,   
I tried:
          ses.DB("db").C("Blog").Find(bson.M{"comments.id":"1111111111111"}).All(&res)
          got expected result

          then try:
          ses.DB("db").C("Blog").Find(bson.M{"id":"000000000000"}).All(&res)
	    got expected result

          And then
          ses.DB("db").C("Blog").Find(bson.M{"id":"000000000000","comments.id": "1111111111111"}).All(&res)
          got nothing

I think I should create a bson object,but don't know how,can anybody give an example.

Alex Luya

unread,
Aug 6, 2012, 2:40:48 AM8/6/12
to mgo
I can't access google groups(blocked here),not sure whether previous question(almost same as this one) has been delivered successfully,this is a modified version.

My data structure like this:
   Blog:
         id                       string
title                    string
         content                  string
              comments:
                           id                string
                           content           string

And if I want to query a comment(with id:000000000000) within a blog(with id:1111111111111),  
I tried:
          ses.DB("db").C("Blog").Find(bson.M{"comments.id":"1111111111111"}).All(&res)
          got expected result

          then try:
          ses.DB("db").C("Blog").Find(bson.M{"id":"000000000000"}).All(&res)
    got expected result

          And then
          ses.DB("db").C("Blog").Find(bson.M{"id":"000000000000","comments.id": "1111111111111"}).All(&res)
          got nothing

I think I should create a bson object,so do it like this:

        query, _ := Marshal([]byte("{'id': '0000000000000','comments.id':'1111111111111')"))
               res := mgd.ExcuteQuery(collection, query)

The query returns 0 result,so can anybody give an example.
.

Alex Luya

unread,
Aug 7, 2012, 3:18:27 AM8/7/12
to mgo-...@googlegroups.com

Gustavo Niemeyer

unread,
Aug 7, 2012, 4:07:44 AM8/7/12
to mgo-...@googlegroups.com, alexand...@gmail.com
Hi Alex,

On Mon, Aug 6, 2012 at 7:40 AM, Alex Luya <alexand...@gmail.com> wrote:
> My data structure like this:
(...)
> ses.DB("db").C("Blog").Find(bson.M{"id":"000000000000","comments.id":
> "1111111111111"}).All(&res)
> got nothing

Something similar to this should work fine, as long as you actually
have objects matching both fields at the same time. If you can't
figure out, I suggest sending a small self-contained Go file that
shows your problem happening so we can help you out.


gustavo @ http://niemeyer.net

Alexander Luya

unread,
Aug 11, 2012, 1:12:53 AM8/11/12
to mgo-...@googlegroups.com, alexand...@gmail.com

//mgd.go

package mgd

import (
     "labix.org/v2/mgo"
)

var (
      mgoSession   *mgo.Session
      databaseName = "TestDB"
)

func cloneSession(method string) *mgo.Session {
    if mgoSession == nil {
         var err error
         mgoSession, err = mgo.Dial("localhost")
        if err != nil {
                    panic("Can't use 'Dial('localhost')' connect to mongodb")
        }
      }
      return mgoSession.Clone()
}


func ExcuteQuery(collection string, query interface{}) []interface{} {
          ses := cloneSession("ExcuteQuery")
          defer ses.Close()

           var result []interface{}
            err := ses.DB(databaseName).C(collection).Find(query).All(&result)
          if err != nil {
               panic(err)
          }
         return result

}


func ExcuteInsert(collection string, model interface{}) {
          ses := ensureMongoConnection("ExcuteInsert")
          defer ses.Close()

          err := ses.DB(databaseName).C(collection).Insert(model)
         if err != nil {
             panic(err)
         }
}


//mgd_test.go
package mgd

import (
        "aws/mgd"
       . "labix.org/v2/mgo/bson"
 .       "launchpad.net/gocheck"
          "testing"
)

func Test(t *testing.T) { TestingT(t) }

type MySuite struct {
              Token    string
              Title    string
              Comments []Comment
}

type Comment struct {
          Token   string
          Content string
}

var (
         _            = Suite(&MySuite{})
        collection   = "test"
        blogToken    = "blog-0"
        title        = "how to make uml ..."
        commentToken = "comment-0"
        content      = "You are..."
)

func (s *MySuite) TestInsert(c *C) {
       mgd.ExcuteInsert(collection, MySuite{blogToken, title, []Comment{Comment{commentToken, content}}})
}

func (s *MySuite) TestGetModel(c *C) {
      res := mgd.ExcuteQuery(collection, M{"Token": blogToken, "Comments.Token": commentToken})
      c.Assert(len(res), Equals, 1)
      //here len(res)==0,but if mgd.ExcuteQuery(nil)...,the len(res)==1

Reply all
Reply to author
Forward
0 new messages