首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用mongoose更新mongodb中的对象?

如何使用mongoose更新mongodb中的对象?
EN

Stack Overflow用户
提问于 2015-09-16 06:35:53
回答 1查看 610关注 0票数 3

我有一个模式:

代码语言:javascript
复制
exports.participant = function(mongodb){
  var participantSchema =  new mongoose.Schema({
   uniqueid: {type: String, unique: true},
   channel: String,
   conference: String,
   calleridnum: String,
   calleridname: String,
   event: String,
   status : { type: Boolean, default: 0 }
},{ collection: 'participants'});

  var participant = mongodb.model('Participant', participantSchema);
  return participant;
}

我已经试过how to update a object in mongodb via mongoose了,但对我来说还是不管用。

我想将状态更新为1

我在这里调用更新代码。。

我的架构的文件位置是:../models/app.js

我更新的文件位置是:../lib/update.js

这是我的更新代码。。

代码语言:javascript
复制
var appInit = require('../app');
var logger = appInit.logger;
var basedir = appInit.basedir;
var connection = appInit.connection;


var mongodb = appInit.mongodb;

var handleAsteriskAmi = require(basedir+'/lib/handleasteriskami');
var asteriskAmi = new handleAsteriskAmi();

var handleHttpProc = require(basedir+'/lib/handlehttpproc');
var httpProc = new handleHttpProc();

function handleSocketio(){
  this.init = function(io){
    io.sockets.on('connection',function(socket){
      if(socket){
        logger.info("socket.io is connected");
      }
      socket.on('disconnect', function () {
        logger.error("Socket.io is disconnected");
      });
      socket.on('action', function(data){
        var result = JSON.parse(JSON.stringify(data));
        asteriskAmi.listenActionConfbridge(result,function(response){
          logger.info(JSON.stringify(response));
          if(response.event=="originate"){
                  io.sockets.emit("response",response);
          }
          if(response.event=="confbridgemute" || response.event=="confbridgeunmute"){
                  io.sockets.emit("response",response);
                  mongodb.model("Participant").update(
                  {channel: response.channel},
                  {$set: {status: 1}}
                  );
          }
          if(response.event=="confbridgelock" || response.event=="confbridgeunlock"){
                  io.sockets.emit("response",response);
          }
        });
      });

    });
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-16 07:53:09

我相信您需要在../models/app.js中的参与者函数中传递一个猫鼬对象,然后您可以使用该对象将participantSchema转换为一个模型。因此,在../models/app.js中,您可以将participant函数实现为

代码语言:javascript
复制
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
exports.participant = function(){
    var participantSchema =  new Schema({
        uniqueid: {type: String, unique: true},
        channel: String,
        conference: String,
        calleridnum: String,
        calleridname: String,
        event: String,
        status : { type: Boolean, default: 0 }
     },{ collection: 'participants'});

    var participant = mongoose.model('Participant', participantSchema);
    return participant;
}

调用../lib/update.js中的参与者模型

代码语言:javascript
复制
var appInit = require('../app');
var Participant = appInit.participant();
Participant.update(
    {channel: response.channel},
    {$set: {status: 1}}
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32601307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档