如何从Socket.IO添加数据到数组(MongoDB)? (的node.js)

我有以下MongoDB架构的

//MongoDB Schema var Line = new Schema({ id : Number, user : String, text : String, }); var Story = new Schema ({ sid: {type: String, unique: true, required: true}, maxlines: {type: Number, default: 10}, // Max number of lines per user title: {type: String, default: 'Select here to set a title'}, lines: [Line], }); 

对于特定的故事,MogoDB中的数据如下所示:

 { __v: 0, _id: ObjectId("5084559945a0a23c1b000002"), lines: [{ "id": 1, "user": "Joe", "text": "This is line number 1" }, { "id": 2, "user": "Adam", "text": "This is line number 2" }, { "id": 3, "user": "John", "text": "This is line number 3" }, ], maxlines: 10, sid: "lJOezsysf", title: "A New Story!" } 

我正在使用Socket.Io与服务器通信,并将消息推送给其他用户

 // when the client emits 'sendline', this listens and executes socket.on('sendline', function (data) { // we tell the client to execute 'updatestory' with 2 parameters io.sockets.in(socket.room).emit('updatestory', socket.username, data); Story.findOne({ sid: socket.room }, function(err, story){ ** What would go here to update MongoDB? ** console.log(socket.room); }); }); 

我如何使用上述从socket.io中添加数据并将其保存到MogoDB?

 socket.room has the same value as sid on Mongo so it finds the correct entry. socket.username = user data = text 

我试图使用下面,保存数据,但我得到一个错误,我想它创build一个新的条目与'下一个ID'行数组。

 Story.Lines.User = socket.username; Story.Lines.text = data; Story.save(); 

我会用什么方法将其推送到数据库?

*编辑*

pipe理得到它的工作,似乎是我使用故事而不是故事。

我使用的代码如下:

  Story.findOne({ sid: socket.room }, function(err, story){ story.lines.push({ user: socket.username, text: data, }); 

 Story.findOne({ sid: socket.room }, function(err, story){ story.foo = socket.room.foo; story.save(function(err, story){ res.send("Story updated"); }); });