0

I want to insert a new item into an embedded array , but before that I need to make sure that that item does not exist already in the array : thats the collection

"Accounts":[
{
    "Account_id" : 70,
    "FirstName" : "name",
    "LastName" : "bffff",
    "Username" : "deee",
    "Password" : "xyzqds@j",
    "AccountType" : "admin",
    "Created_at" : "01-01-2018",
    "Rules" : {
        "Goal" : 480,
        "DoNotDisturbFrom" : "22",
        "DoNotDisturbTo" : "8",
        "Frequency" : "weekly"
    }
},
{
    "Account_id" : 52,
    "FirstName" : "joe",
    "LastName" : "Doe",
    "Username" : "aajengui1",
    "Password" : "abc@j",
    "AccountType" : "user",
    "Created_at" : "01-02-2018",
    "Rules" : {
        "Goal" : 260,
        "DoNotDisturbFrom" : "10",
        "DoNotDisturbTo" : "12",
        "Frequency" : "monthly"
    }
}

]

I want to append a new account into the array but it has to be unique so it's like verifying that the username / email does not exist already

frod_junior
  • 115
  • 2
  • 13
  • Possible duplicate of [Can you specify a key for $addToSet in Mongo?](https://stackoverflow.com/questions/14527980/can-you-specify-a-key-for-addtoset-in-mongo) – Muhammad Usman May 07 '18 at 14:29

2 Answers2

0

Your schema design has a major limitation: if you have enough accounts, the document could be larger than 16 MB, which is the maximum document size in MongoDB.

If applicable to your use case, a better solution is to create accounts as a separate collection, instead of being an array in a single document. This will achieve two things:

  1. You will not be limited to the 16 MB limitation if you have enough accounts in your system.
  2. To add a new account while verifying that the new account username is unique, you can create a unique index on the accounts collection. Trying to insert a document with a duplicate username will result in an error.
kevinadi
  • 13,365
  • 3
  • 33
  • 49
  • thanks for the explanation , but I dont think that's a problem coz the nb of accounts is limited to 5 in which there is one and only Admin as for my problem I manged to overcome it using my answer – frod_junior May 08 '18 at 07:47
0

I actually managed to do this that solved m problem :

data = request.get_json()
    findOneuser=customers.find({"Customer_id": customer_id, "Accounts.Username": data["Username"]},
         {"Accounts.Username.$": 1, "_id": 0})
    print(findOneuser.count())
    if findOneuser.count() == 0:
        customers.update(
            {"Customer_id": customer_id},
            {"$addToSet": {
                "Accounts":
                    {
                        "Account_id": data["Account_id"],
                        "FirstName": data["FirstName"],
                        "LastName": data["LastName"],
                        "Username": data["Username"],
                        "AccountType": "user",
                        "Created_at": data["Created_at"]} 

here it's an Api where I get the new account data in JSON format

> findOneuser

will verify if the user exists than append it to the accounts array

frod_junior
  • 115
  • 2
  • 13