mongoose更新对象类型模式

时间:2018-01-09 20:28:17

标签: node.js mongodb

我想更新我的数据库但是如何实现这一目标。 我能找到合适的文件,但由于'或'条件,我不知道如何更新。有什么建议吗?

我的用户架构

<?php

// check if the flexible content field has rows of data
if( have_rows('project_flexible') ):

// loop through the rows of data
while ( have_rows('project_flexible') ) : the_row();

if( get_row_layout() == 'project_hero_image'):
  $hero_image = get_sub_field('image'); ?>

// Added this if statement
<?php if(is_single()){ ?>
  <div class="hero-image"><img src="<?php echo $hero_image; ?>"></div>
<?php } ?>

<?php
  endif;
  endwhile;

  else :
   // no layouts found
  endif;
?>

app.js

var userSchema = new mongoose.Schema({
    local:{
        localId: {type:Number},
        username:{type:String},
        password: {type:String},
        email:{type:String}     
    },
    facebook:{
        facebookId: {type:Number},
        email:{type:String},
        username:{type:String},
        password: {type:String}       
    },
    twitter:{
        twitterId: {type:Number},
        username:{type:String},
        password: {type:String},  
        email:{type:String}       

    }
});

1 个答案:

答案 0 :(得分:0)

尝试'$ set'电子邮件字段。

   User.findOneAndUpdate({$or: [
          {'local.username': username }, // 'or' condition works perfect
       { 'facebook.username': username },
        { 'twitter.username': username },
]},{
  $set:{email:req.body.email} // doesn't work...here i don't know what to do... 
},function(err, user) {
        if(err){console.log(err);} 
          if(!user){
            console.log('cannot find user');
          }
          if(user){              
            console.log(user);
               return res.send(JSON.stringify(user));

        }
    });