0

I want to switch to MongoDB for my database system. I have a Player class and I basically want to save that to the database. In the player class is a name, rank and a list of purchased items. What is the best way to store all this in MongoDB? I can create a document for the player but are you able to store a list in there? Thanks.

3 Answers 3

1

Sure you can store list, look at MongoDB embedded documents

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

Comments

0

Yeah you can store arrays into mongodb first create a player document like this

db.player.insert({name:"xxx",rank:1});

you can insert the empty list at the time of creation like below or it will dynamically create when you update the document.

 db.player.insert({name:"xxx",rank:1,purchases:[]});//empty array optional

after creating the document you can update document with array like this

db.player.update({name:"xxx"},{$push:{purchases:["t-shirt","shoes"]});

It will create document like this:

{ "_id" : 1234,
  “name": “xxxx”,
  "rank": 1,
  “purchases”:
  [
           "t-shirt",
           "shoes"  
  ]
}

Comments

0

Yes, you can use and store Arrays in MongoDB.

Check out this article. It will help you a lot: http://blog.mongolab.com/2013/04/thinking-about-arrays-in-mongodb/

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.