【发布时间】:2013-12-18 15:50:38
【问题描述】:
我正在尝试设置一个包含房间对象的 mongodb。谁能明白为什么只有垃圾被添加到我的数据库中?我的房间对象包含 5 个房间,因此添加了正确数量的东西,只是没有正确添加。
这就是我设置数据库的方式:
var setupRoomDB = function(){
var roomSchema = mongoose.Schema({
title: String,
description: String,
exitList: [String]
});
Room = mongoose.model("Room", roomSchema);
addRoomsJS();
}
db.once('open', setupRoomDB);
这很好用。现在我想用这个对象中包含的东西填充我的数据库:
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
这就是我尝试这样做的方式:
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);
}
}
当我查看存储在我的数据库中的内容时,这就是我得到的:
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 }
【问题讨论】:
-
对于那些将其标记为重复的人——虽然其中一部分实际上是重复的,但实际问题与此无关。她不明白为什么她认为要保存的数据不存在。
标签: javascript node.js mongodb