21

I want to push some values into array using Python.
Maybe next time when i update the array ,it will insert some values exists ,so it will got some duplicate values.
I want to know is there anyway to avoid duplicate values.
Should I use db.collection.find() to determine whether I should insert or not ?

db.graph.insert_one({"user_id": a.url}, )
for j in a.followers:
    db.graph.update({"user_id": a.url}, {"$push": {"following": j.url}})

3 Answers 3

36

The best way to do this is using $addToSet operator which ensures that there are no duplicate items added to the set and the $each modifier to add multiple values to the"following" array.

urls = [j.url for j in a.followers]
db.graph.update_one({"user_id": a.url}, {"$addToSet": {"following": {"$each": urls}}})

also you should use the update_one method because update is deprecated.

Sign up to request clarification or add additional context in comments.

3 Comments

is it possible with findOneAndUpdate ?
how do you remove ?
Just a side note, this might work well for the example in question - with primitive value types (string, integer) - but might not work well with arrays containing documents. In such a case, comparison done for the whole document and checks if existing documents have the same properties, exactly in the same order and exactly the same values. Meaning, you can't use some unique property of document in an array.
6

I think, that you can use $addToSet operator: https://docs.mongodb.org/manual/reference/operator/update/addToSet/

1 Comment

Try to give an example, rather than just a URL. That way, answers aren't reliant on URLs remaining valid.
0

I'm lazy ass, so I'm going to paste here the code i use. Don't mind the language. The underline logic is similar.

// array of object prevent duplication 1------------------------------

Dict_target_schema.index(
  { user_id: 1, wid: 1, w: 1, "exp.r_id": 1 },
  { unique: true }
);
let result = await Dict_target.findOneAndUpdate(
  { user_id, wid, w, "exp.r_id": { $ne: article_id } },
  {
    $push: {
      exp: {
        r_id: article_id,
      },
    },
  },
  {
    upsert: true,
    new: true,
  }
);

// array of object prevent duplication 2-----------------

Dict_target_schema.index(
  { user_id: 1, wid: 1, w: 1 },
  { unique: true }
);
let result = await Dict_target.findOneAndUpdate(
  {
    user_id,
    wid,
    w,
    exp: { $not: { $elemMatch: { r_id: article_id } } },
  },
  {
    $push: {
      exp: {
        r_id: article_id,
      },
    },
  },
  {
    upsert: true,
    new: true,
  }
);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.