skip to Main Content

I have document with this structure . I need to change the Number 3 ,Pending –> Done
is there an possible ways of doing that?

db.Queue.find({"dr-name":"1guna"},{ "queue.0":1 })

doesn’t return any thing

{   _id: ObjectId('6623b76f360aa86eb3560898'),   queue: [     [],     [],     [],     [],     []   ] }
[{
  "_id": {
    "$oid": "6623b76f360aa86eb3560898"
  },
  "dr-name": "1guna",
  "live-number": "0",
  "est-number": "17",
  "queue": [
    [
      "1",
      "Banduka_Chamaras",
      "07:16:43",
      "pending",
      "1711111919"
    ],
    [
      "2",
      "chicken_Chamaras",
      "07:17:57",
      "pending",
      "0711111116"
    ],
    [
      "3",
      "butter_Chamaras",
      "07:29:11",
      "pending",
      "0711111516"
    ],
    [
      "4",
      "kaslisda_sds",
      "06:33:47",
      "pending",
      "5433454534"
    ],
    [
      "5",
      "kaslisda_sds",
      "07:13:42",
      "pending",
      "5433454539"
    ]
  ]
}]

2

Answers


  1. use pymongo

    pip install pymongo
    

    sample code for updating elements:

    from pymongo import MongoClient
    
    # Connect to the MongoDB database server (change 'localhost' and '27017' as necessary)
    client = MongoClient('mongodb://localhost:27017/')
    
    # Connect to the specific database and collection
    db = client['your_database_name']  # Replace 'your_database_name' with the name of your database
    collection = db['Queue']
    
    # Query to find the document
    query = {"dr-name": "1guna", "queue.2.0": "3"}
    
    # Update statement to change the status
    update = {"$set": {"queue.2.3": "Done"}}
    
    # Perform the update
    result = collection.update_one(query, update)
    
    # Output the result of the update
    print("Documents updated:", result.modified_count)
    
    # Close the connection
    client.close()
    

    UPDATE:

    Update an element with mongodb:

    db.Queue.updateOne(
      { "_id": ObjectId("6623b76f360aa86eb3560898"), "dr-name": "1guna" },
      { $set: { "queue.2.3": "Done" } }
    )
    
    Login or Signup to reply.
  2. The index of an array key is determined by the query on it. Therefore the array key must be included in the query. Please see the below examples.

    test> db.test.drop();
    test> db.test.insertOne({_id:1,a:[[1],[2],[3]]});
    test> db.test.find();
    [
      { _id: 1, a: [ [ 1 ], [ 2 ], [ 3 ] ] }
    ]
    
    // the query { "a":[3] } matches and therefore prints the element at the
    // index 2
    test> db.test.find({_id:1,"a":[3]},{"a.$":1});
    [
      { _id: 1, a: [ [ 3 ] ] }
    ]
    
    // the query { "a":[4] } does not match and therefore it does not print 
    // the element 
    db.test.find({_id:1,"a":[4]},{"a.$":1});
    //no output
    
    // this will thrown an error since the query does not include the array 
    // key in projection, therefore the positional operator $ errors.
    test> db.test.find({_id:1},{"a.$":1});
    //MongoServerError: Executor error during find command :: caused by :: 
    //positional operator '.$' couldn't find a matching element in the array
       
    // Again the array key is not in the query, therefore it will not find 
    // the item at 0th index, as a result no match will print 
    test> db.test.find({_id:1},{"a.0":1});
    [ { _id: 1, a: [ [], [], [] ] } ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search