Example #1
0
func main() {
	mgo.SetDebug(true)
	mgo.SetLogger(log.New(os.Stderr, "", log.LstdFlags))
	session, err := mgo.Dial("localhost")
	if err != nil {
		fmt.Printf("%v", err)
		os.Exit(1)
	}
	var r bson.M
	db := session.DB("test")

	//	err = db.C("bar").Find(nil).One(&r)
	//	if err != nil {
	//		fmt.Printf("%v", err)
	//		os.Exit(1)
	//	}
	//	fmt.Printf("%#v\n", r)

	data, reply, err := db.QueryOp(&mgo.QueryOp{Collection: "test.bar", Limit: 1})
	if err != nil {
		fmt.Printf("%v", err)
		os.Exit(1)
	}
	fmt.Printf("%v, %#v\n", data, reply)
	for _, d := range data {
		err = bson.Unmarshal(d, &r)
		if err != nil {
			fmt.Printf("%v", err)
			os.Exit(1)
		}
		fmt.Printf("%#v\n", r)
	}
}
Example #2
0
func newPlayOpChan(fileName string) (<-chan *mongocaputils.OpWithTime, error) {
	opFile, err := os.Open(fileName)
	if err != nil {
		return nil, err
	}
	ch := make(chan *mongocaputils.OpWithTime)
	go func() {
		defer close(ch)
		for {
			buf, err := mongoproto.ReadDocument(opFile)
			if err != nil {
				fmt.Printf("ReadDocument: %v\n", err)
				if err == io.EOF {
					return
				}
				os.Exit(1)
			}
			var doc mongocaputils.OpWithTime
			err = bson.Unmarshal(buf, &doc)
			if err != nil {
				fmt.Printf("Unmarshal: %v\n", err)
				os.Exit(1)
			}
			ch <- &doc

		}
	}()
	return ch, nil
}
Example #3
0
func (op *OpReply) String() string {
	docs := make([]string, 0, len(op.Documents))
	var doc interface{}
	for _, d := range op.Documents {
		_ = bson.Unmarshal(d, &doc)
		jsonDoc, err := bsonutil.ConvertBSONValueToJSON(doc)
		if err != nil {
			return fmt.Sprintf("%#v - %v", op, err)
		}
		asJSON, _ := json.Marshal(jsonDoc)
		docs = append(docs, string(asJSON))
	}
	return fmt.Sprintf("OpReply %v %v", op.Message, docs)
}
Example #4
0
func (op *OpQuery) String() string {
	var query interface{}
	if err := bson.Unmarshal(op.Query, &query); err != nil {
		return "(error unmarshalling)"
	}
	queryAsJSON, err := bsonutil.ConvertBSONValueToJSON(query)
	if err != nil {
		return fmt.Sprintf("ConvertBSONValueToJSON err: %#v - %v", op, err)
	}
	asJSON, err := json.Marshal(queryAsJSON)
	if err != nil {
		return fmt.Sprintf("json marshal err: %#v - %v", op, err)
	}
	return fmt.Sprintf("OpQuery %v %v", op.FullCollectionName, string(asJSON))
}
Example #5
0
func (op *OpQuery) Execute(session *mgo.Session) error {
	fmt.Printf("query \n")
	nsParts := strings.Split(op.FullCollectionName, ".")
	coll := session.DB(nsParts[0]).C(nsParts[1])
	queryDoc := bson.M{}
	err := bson.Unmarshal(op.Query, queryDoc)
	if err != nil {
		return err
	}
	query := coll.Find(queryDoc)
	query.Limit(int(op.NumberToReturn))
	query.Skip(int(op.NumberToSkip))
	result := []bson.M{}
	err = query.All(&result)
	if err != nil {
		fmt.Printf("query error: %v\n", err)
	}
	return err
}