Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

problem with updating document's field that was used to query documents

27 views
Skip to first unread message

Luke

unread,
Aug 15, 2017, 5:03:34 PM8/15/17
to mgo-users
Hi All,

I have a weird problem. I am querying docs using below query:


query := bson.M{
            "brandid": 1003,
            "$or": []bson.M{
                bson.M{
                    "st": bson.M{
                        "$exists": false,
                    },
                },
                bson.M{
                    "mt": bson.M{
                        "$gte": time.Date(2016, time.November, 10, 23, 0, 0, 0, time.UTC),
                    },
                },
            },
        }



Then I am looping through the result and I am updating the "st" field, for example:

col.Update(tran.Id,bson.M{"$set":bson.M{"st":time.Now()}})


The Find query returns 12K documents when I do 

col.Find(query).Count()

, however when I loop through the result, the loop keeps going forever and after 12k documents, I start getting the same documents that I have already updated. Here is an example of the loop:

col := s.db.C("transactions")
data := col.Find(query)
c, _ := data.Count()
iter := data.Iter()
i := 0

RETRIEVER_LOOP:
for {
tran := Transaction{}
if !iter.Next(&tran) {
break RETRIEVER_LOOP
}
i++
fmt.Printf("\rDone docs: %v of ", i, c)
                //DO some work here
                col.Update(tran.Id,bson.M{"$set":bson.M{"st":time.Now()}})
        }



Did anybody had a similar issue? 


If I remove from the query 

bson.M{
                    "st": bson.M{
                        "$exists": false,
                    },
                },


thank everything works as expected



Thank, 
Luke
Reply all
Reply to author
Forward
0 new messages