good morning!!!!

Skip to content
Snippets Groups Projects
Commit ec41cbeb authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

The old Collection.UpsertId() trick to get the _id value of the latest...

The old Collection.UpsertId() trick to get the _id value of the latest inserted item stopped to work. This workaround allocates a new _id and then updates the item.
parent 677cb3e6
No related branches found
No related tags found
Loading
......@@ -196,17 +196,19 @@ func (self *Collection) Truncate() error {
// Appends an item (map or struct) into the collection.
func (self *Collection) Append(item interface{}) (interface{}, error) {
var err error
var id bson.ObjectId
// Dirty trick to return the Id with ease.
res, err := self.collection.UpsertId(nil, item)
// Let's create an empty item to allocate an ID.
id = bson.NewObjectId()
if err != nil {
if err = self.collection.Insert(bson.M{"_id": id}); err != nil {
return nil, err
}
if res.UpsertedId != nil {
id = res.UpsertedId.(bson.ObjectId)
// Now append data the user wants to append.
if err = self.collection.Update(bson.M{"_id": id}, item); err != nil {
return nil, err
}
return id, nil
......
......@@ -39,8 +39,10 @@ import (
const wrapperName = "mongo"
// Wrapper settings.
const host = "127.0.0.1"
const dbname = "upperio_tests"
const (
host = "testserver.local"
dbname = "upperio_tests"
)
// Global settings for tests.
var settings = db.Settings{
......@@ -91,6 +93,7 @@ func TestEnableDebug(t *testing.T) {
}
// Trying to open an empty datasource, it must succeed (mongo).
/*
func TestOpenFailed(t *testing.T) {
_, err := db.Open(wrapperName, db.Settings{})
......@@ -98,6 +101,7 @@ func TestOpenFailed(t *testing.T) {
t.Errorf(err.Error())
}
}
*/
// Truncates all collections.
func TestTruncate(t *testing.T) {
......@@ -175,6 +179,10 @@ func TestAppend(t *testing.T) {
"name": "Ozzie",
})
if err != nil {
t.Fatalf("Append(): %s", err.Error())
}
if id == nil {
t.Fatalf("Expecting an ID.")
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment