Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have such MongoDB Collection:

{
  date: date,
  domain: domain,
  visitors:  [ {owner:owner, ip:ip, views:views} ]
}

Now I want to check where date equals date AND domain equals domain it should delete the whole visitors array, e.g.:

collection.update({"date":date, {"domain":domain} {"$pull":{"visitors"}} )

How to make this work?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
233 views
Welcome To Ask or Share your Answers For Others

1 Answer

You could try use the $set operator to empty the visitors array instead of removing all items by using the $pull operator, which would be much faster as the $pull will have to do calculations on arrays:

db.collection.update( {"date": date, "domain": domain}, { $set : {"visitors": [] }} , {multi: true} )

The equivalent $pull operation would be

db.collection.update( {"date": date, "domain": domain}, { $pull : { "visitors": {} }}, {multi: true} )

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...