0

I am trying to set up a mongodb that holds room object. Can anyone see why there is only garbage being added to my database? my rooms object holds 5 rooms, so the correct amount of things are being added, they just aren't being added properly.

This is how I setup my db:

var setupRoomDB = function(){
    var roomSchema = mongoose.Schema({
        title: String,
        description: String,
        exitList: [String]
    });

    Room = mongoose.model("Room", roomSchema);
    addRoomsJS();
}
db.once('open', setupRoomDB);

This works great. Now I want to populate my database with things contained in this object:

var rooms = {
    bridge: {
    title: "Bridge",
    description: "You are on the Bridge.  There are big comfy chairs and a big screen here.",
    roomExits: ['sickbay'],
     },

     engineering: {
    title: "Engineering",
    description: "You are in Engineering.  There are lots of funny instruments, many     smaller screens, and kind of uncomfortable chairs.",
    roomExits: ['sickbay'],
   },
etc

This is how I am trying to do this:

var addRoomsJS = function (){

    for (var room in rooms){
        var addRoom = function (err, rooms){
            //if the room is already contained
            if (rooms.length!=0){
                //res.redirect("/?error=room already exists");  
                return;
            }

            var newRoom = new Room({
                title:room.title,
                description : room.description,
                roomExits: room.roomExits
            });
                newRoom.save();
        };
        Room.find({title:room.title}, addRoom);
    }
}

When I view what is being stored in my db this is what I get:

sarah@superawesome:~/comp2406/adventure-ajax-demo$ mongo
MongoDB shell version: 2.4.6
connecting to: test
> show dbs
local   0.078125GB
test    (empty)
users   0.203125GB
> use users
switched to db users
> show collections
rooms
system.indexes
> db.rooms.find()
{ "_id" : ObjectId("529cd5686f854f1512000001"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000002"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000003"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000004"), "exitList" : [ ], "__v" : 0 }
{ "_id" : ObjectId("529cd5686f854f1512000005"), "exitList" : [ ], "__v" : 0 }
Sarah
  • 2,713
  • 13
  • 31
  • 45
  • 1
    To those who marked this as a duplicate -- while part of it was, in fact, a duplicate, the actual problem wasn't anything to do with that. She didn't understand why the data she thought was saving, wasn't there. – WiredPrairie Dec 04 '13 at 02:16

2 Answers2

3
  1. You've changed the name of the property from the schmea it's called exitList. Yet, when you create the room it's called roomExits.
  2. The way you're trying to save the data is not capturing the value of the room in a closure and it's not grabbing the actual object from the rooms object.
  3. The __v is Mongoose's versionKey (Reference and how to disable)

When you use for(var x in obj), x is the name of the key, but not the value. So, in your example, it would just be bridge as a string. You'd need to grab the actual object from the rooms object:

var r=rooms[room];

You could wrap the contents of the loop with a closure and grab the value:

for (var room in rooms) {
    (function(room) {
       var addRoom = ... /* your code here */
    })(rooms[room]);
} 

If you don't use a closure, by the time the async find function returns, the value of room will not only just be a string, it will be the last value in the associative array (whatever the last property processed by the in was).

Community
  • 1
  • 1
WiredPrairie
  • 58,954
  • 17
  • 116
  • 143
  • While this didn't solve my problem, I do now have a better understanding of what is going on, so thank you. I will update my question with the answer when I finally figure it out - I have been able to place the correct items in the database, but I am adding duplicates now. – Sarah Dec 04 '13 at 17:22
  • What was the result if this didn't work? I don't understand why this didn't answer your question? – WiredPrairie Dec 04 '13 at 17:37
0

I don't know what you mean with garbage, but:

  • MongoDB allocates database files in chunks, thats why your database is 0.2 GB large. Until you actually have 0.2 GB of data the size will (mostly) stay the same. After you hit that limit the database will be allocate the next big chunk.
  • The __v field is created by Mongoose and used for checks if you try to edit the array from 2 different processes.
TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • should I not be able to read what is actually in my database? Like shouldn't it print something like : Title: Bridge, Description: blah, roomList: sickbay, etc ? – Sarah Dec 02 '13 at 20:22
  • what does mongodb data file allocation have to do with the question? file allocation is completely transparent from the point of view of application/shell/etc. – Asya Kamsky Dec 03 '13 at 03:33
  • @AsyaKamsky This question is very vague. Since OP explicitly added the results of the database sizes to his question, I thought he might be wondering why his databases are that large. – TheHippo Dec 03 '13 at 09:56
  • see the other answer - it actually explains why OP is trying to save certain data but is getting something else saved instead. nothing to do with sizes of data files or sizes of the database. – Asya Kamsky Dec 04 '13 at 04:03