简体   繁体   English

使用 Mongoose 在 MongoDb 的一个属性中插入所有即将到来的数据

[英]Insert all upcoming data in one property in MongoDb using Mongoose

My Upcoming data is :我即将到来的数据是:

{
  id: 123
  contact: 4591112365
  address: Block C New York 
  firstname: John
  lastname: Wick

}

Now on inserting data using Mongoose on Mongodb, I want my data to be inserted as this:现在在 Mongodb 上使用 Mongoose 插入数据,我希望我的数据插入如下:

{
id: 123
contact: 4591112365
information:{
    address: Block C New York 
    firstname: John
    lastname: Wick
   }
}

I am using NodeJs and don't want to programmatically map the data, I want when I insert data in the above format it will automatically map to below one, I Think Using mongoose schema and virtual function or virtual ID I can do that but I am not sure about how to do that.我正在使用 NodeJs 并且不想以编程方式映射数据,我希望当我以上述格式插入数据时,它会自动映射到以下格式,我认为使用猫鼬模式和虚拟函数或虚拟ID 我可以这样做,但我我不确定如何做到这一点。

 //make model const mongoose = require("mongoose"); const infoSchema = new mongoose.Schema({ contact:{ type:Number } id:{ type:Number }, information: { address: { type:String, }, firstName: { type:String, }, lastName: { type:String, }, }, }) module.exports = mongoose.model("User", infoSchema); // make new user by importing that model into this js file const User= require('/path/to/userModel') const user = new User({ id:req.body.id, contact:req.body.contact, "information.address":req.body.address "information.firstName":req.body.firstName "information.lastName":req.body.lastName }); savedUser=user.save()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM