Sign In Register

How can we help you today?

Start a new topic

How to find and modify a particular field in a array in a json document

 Eg. JSON

{
 "cityName" : "cityA",
 "population": 340000,
 "activeplayers": 
 [
  {
   "name": "himan",
   "playerid": "2323233",
   "peoplecount": 23232,
   "healthy": 232,
   "infected": 333
  }
 ],

 "players":
 [
  {
   "name": "himan",
   "playerid": "2323233",
   "peoplecount": 23232,
   "healthy": 232,
   "infected": 333
  }
 ]

}

 If i have to find a record for particular player in active players array 

eg. player with name 'himan' in the activeplayers array.  The activeplayer array grows as the no of player in city grows but every city has a limit of 50 concurrent players. 

So how it is possible without updating the whole document or parsing through the whole document.


1 person has this question

In mongoDB you would do something like: {activePlayers: {$elemMatch: {name: "himan"}}}

Ok but $elemMatch will return the other array- players which may contain lots of entries.

Also how would i modify the particular array that i got from the  $elemMatch query.

$elemMatch filters the document to just the array element when used as the projection argument . Sorry for  the confusion. 

Now the question is -

What is the efficient way to modify the array element and store it back in its respective position?

How to delete the array element from the document?

How to insert a new element in the array?


Sorry for lost of questions but i noob in this stuff. 

Login to post a comment