mongodb - Not finding documents using golang's mgo with partial attributes -
i'm trying remove bunch of documents have common attribute. document looks like:
{ _id : { attr1 : 'foo', attr2 : 'bar' }, attr3 : 'baz', }
more 1 document have same 'foo' value in attr1 entry. i'm trying remove of those. i've got similar this:
type docid struct { attr1 string `bson:"attr1,omitempty"` attr2 string `bson:"attr2,omitempty"` } type doc struct { id docid `bson:"_id,omitempty"` attr3 string `bson:"attr3,omitempty"` } doc := doc{ id : docid{ attr1 : 'foo' }, } collection := session.db("db").c("collection") collection.remove(doc)
the problem here i'm getting not found
error in remove call. can see odd in code?
thanks lot!
this consequence of way mongodb handles exact match , partial match. can demonstrated using mongo shell:
# here documents > db.docs.find() { "_id" : { "attr1" : "one", "attr2" : "two" }, "attr3" : "three" } { "_id" : { "attr1" : "four", "attr2" : "five" }, "attr3" : "six" } { "_id" : { "attr1" : "seven", "attr2" : "eight" }, "attr3" : "nine" } # test exact match: works fine > db.docs.find({_id:{attr1:"one",attr2:"two"}}) { "_id" : { "attr1" : "one", "attr2" : "two" }, "attr3" : "three" } # let's remove attr2 query: nothing matches anymore, # because mongodb still thinks query requires exact match > db.docs.find({_id:{attr1:"one"}}) ... nothing returns ... # , proper way query partial match: works fine. > db.docs.find({"_id.attr1":"one"}) { "_id" : { "attr1" : "one", "attr2" : "two" }, "attr3" : "three" }
you find more information topic in documentation.
in go program, suggest use following line:
err = collection.remove(bson.m{"_id.attr1": "foo"})
do not forget test errors after each roundtrip mongodb.