Node.js 在不同时间向MongoDB模型添加数据

Node.js 在不同时间向MongoDB模型添加数据,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我对Mongdob和mongoose有很好的理解,但这一点让我困惑了一段时间。我有一个user.js模型,带有用户名、密码等(所有基本的用户信息)。此数据在用户注册帐户时添加。但是每个用户也有更多的数据链接到它,这些数据在注册时没有创建或添加 这是我的模型: // User Schema const UserSchema = new Schema({ // PERSONAL USER INFO username: { type: String,

我对Mongdob和mongoose有很好的理解,但这一点让我困惑了一段时间。我有一个user.js模型,带有用户名、密码等(所有基本的用户信息)。此数据在用户注册帐户时添加。但是每个用户也有更多的数据链接到它,这些数据在注册时没有创建或添加

这是我的模型:

// User Schema
const UserSchema = new Schema({

    // PERSONAL USER INFO
    username: {
        type: String,
        index: true
    },
    email: {
        type: String
    },
    password: {
        type: String
    },

    // INSTAGRAM ACCOUNT INFORMATION
    ig_username: {
        type: String
    },
    ig_password: {
        type: String
    },
    story_price: {
        type: Number
    },
    fullpost_price: {
        type: Number
    },
    halfpost_price: {
        type: Number
    },
    leads: [{
        title: { type: String }
    }]
});

// EXPORTS
const User = module.exports = mongoose.model('user', UserSchema);
除“Lead”之外的所有字段都是在注册时创建的。但我想用另一张表格填写Leads字段。我尝试了.update()、.save()、$set、$push以及各种方法,但无法使其正常工作

我找到的大多数解决方案都使用
var user=new user({…})
创建一个新用户,然后在添加额外数据后使用
.save()
。但这似乎是错误的,因为已经创建了用户,我只是尝试将数据添加到一个附加字段中


我想我只是在掩饰一些基本的东西,但如果有办法做到这一点,我会很高兴听到它。谢谢

我会为潜在客户创建一个子模式

// Create a sub-schema for leads
const leadsSubSchema = new Schema({
  title: {
    type: String,
  },
});

// Create a schema for user
const UserSchema = new Schema({
  username: {
    type: String,
    index: true
  },

  // ...

  leads: [leadsSubSchema]
});

// EXPORTS
const User = module.exports = mongoose.model('user', UserSchema);

然后更新

User.update({
  _id: user_id,
}, {
  $push: {
    leads: lead_to_add,
  },
});

类似于
User.update({username:some User},{$push:{leads:new lead}}})
@Veeram我以前尝试过这个方法,但是当我在mongo shell中检查用户时,它不会创建“leads”文档。用户看起来完全一样。