簡體   English   中英

使用 Mongoose 在 MongoDb 的一個屬性中插入所有即將到來的數據

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

我即將到來的數據是:

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

}

現在在 Mongodb 上使用 Mongoose 插入數據,我希望我的數據插入如下:

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

我正在使用 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