Skip to content

Commit

Permalink
Update function
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejs committed Nov 20, 2009
1 parent e6ce7b5 commit d943789
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
46 changes: 46 additions & 0 deletions mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,28 @@ func (coll *Collection) FindOne(query BSON) (BSON, os.Error) {
return cursor.GetNext();
}

func (coll *Collection) update(um *updateMsg) os.Error {
um.requestID = rand.Int31();
conn := coll.db.conn;
return conn.writeMessage(um);
}

func (coll *Collection) Update(selector, document BSON) os.Error {
return coll.update(&updateMsg{coll.fullName(), 0, selector, document, 0})
}

func (coll *Collection) Upsert(selector, document BSON) os.Error {
return coll.update(&updateMsg{coll.fullName(), 1, selector, document, 0})
}

func (coll *Collection) UpdateAll(selector, document BSON) os.Error {
return coll.update(&updateMsg{coll.fullName(), 2, selector, document, 0})
}

func (coll *Collection) UpsertAll(selector, document BSON) os.Error {
return coll.update(&updateMsg{coll.fullName(), 3, selector, document, 0})
}

func (db *Database) Command(cmd BSON) (BSON, os.Error) {
coll := db.GetCollection("$cmd");
return coll.FindOne(cmd);
Expand Down Expand Up @@ -381,3 +403,27 @@ func parseReply(b []byte) *replyMsg {

return r;
}

type updateMsg struct {
fullCollectionName string;
flags int32;
selector, document BSON;
requestID int32;
}

func (u *updateMsg) OpCode() int32 { return _OP_UPDATE }
func (u *updateMsg) RequestID() int32 { return u.requestID }
func (u *updateMsg) Bytes() []byte {
buf := bytes.NewBuffer(make([]byte, 4));
buf.WriteString(u.fullCollectionName);
buf.WriteByte(0);

b := make([]byte, 4);
binary.LittleEndian.PutUint32(b, uint32(u.flags));
buf.Write(b);

buf.Write(u.selector.Bytes());
buf.Write(u.document.Bytes());

return buf.Bytes();
}
7 changes: 6 additions & 1 deletion mongo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type IndexCmd struct {
Key *KeyStruct;
}

func TestInsert(t *testing.T) {
func TestStuff(t *testing.T) {
obj, err := mongo.BytesToBSON([]byte{92, 0, 0, 0, 1, 115, 101, 99, 111, 110, 100, 0, 0, 0, 0, 0, 0, 0, 0, 64, 3, 102, 105, 102, 116, 104, 0, 23, 0, 0, 0, 2, 118, 0, 2, 0, 0, 0, 101, 0, 2, 102, 0, 2, 0, 0, 0, 105, 0, 0, 3, 102, 111, 117, 114, 116, 104, 0, 5, 0, 0, 0, 0, 2, 116, 104, 105, 114, 100, 0, 6, 0, 0, 0, 116, 104, 114, 101, 101, 0, 16, 102, 105, 114, 115, 116, 0, 1, 0, 0, 0, 0});
assertTrue(err == nil, "failed parsing BSON obj", t);

Expand All @@ -44,6 +44,11 @@ func TestInsert(t *testing.T) {
assertTrue(doc.Get("fourth").Kind() == mongo.ObjectKind, "returned doc has proper 'fourth' element", t);
assertTrue(doc.Get("fifth").Get("f").String() == "i" && doc.Get("fifth").Get("v").String() == "e", "returned doc has proper 'fifth' element", t);

newDoc, _ := mongo.Marshal(map[string]string{"first": "one", "second": "two", "third": "three"});
coll.Update(q, newDoc);
doc, _ = coll.FindOne(q);
assertTrue(doc.Get("first").String() == "one", "update", t);

rem, _ := mongo.Marshal(map[string]string{"third": "three"});
coll.Remove(rem);
doc, err = coll.FindOne(rem);
Expand Down

0 comments on commit d943789

Please sign in to comment.