最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

如何添加模式,以另一个模式阵列?

IT培训 admin 6浏览 0评论

如何添加模式,以另一个模式阵列?

我有地址模式和客户模式。我有我的客户架构中的一个元素的地址阵列。我将派遣一个ADRESS模型作为我的REQ身体和客户ID REQ PARAM。我怎么能保存到ADRESS它不会忽略客户是内部架构声明数组?

这是我的客户架构

const customerSchema = mongoose.Schema ({
  _id: mongoose.Schema.Types.ObjectId,
  name: String,
  phone_number: String,
  password: String,
  type:{type:String,enum:['admin','user']},
  adresses:['Adress'],
  orders:[{type: mongoose.Schema.Types.ObjectId, ref: 'Order'}]
});

这是我的地址模式

const addressSchema= mongoose.Schema({
    _id:mongoose.Types.ObjectId,
    postalCode:Number,
    city:String,
    district:String,
    neighborhood:String,
    streetNumber:String,
    no:Number,
    buildingName:String,
    apartmentNumber:Number,
    floor:Number,
    coordinates:{
        latitude:Number,
        longitude:Number
    },
    addressName:String,
    customerId: {type: mongoose.Schema.Types.ObjectId,ref:'Customer'}


}); 

我想不出我应该如何去做到这一点。我发现,我会去把我的地址,像这样的客户。

这是我如何获得特定客户

Customer.find({_id:req.params.customerId},(err,data)=>{
    if(err) return next(err);
    else{
      //What I am going to do here?
    }
});

首先,我应该把什么类型的地址阵列是客户架构里面里面?

第二个发现这我要添加地址给客户,我该怎么做之后?猫鼬5.4.11文件是不够的我。此链接似乎我需要的东西,但我不知道如何做到这一点的问题。

.html

回答如下:

好了,基本上就是你寻找的是:关联。您需要建立用户和客户的模型之间的连接。

我们会说,应对例如ID属于用户和用户参考地址对象。

考虑一个例子:

const personSchema = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  age: Number,
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = Schema({
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  title: String,
  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);

现在,让我们尝试作者分配给特定的创建故事:

const author = new Person({
  _id: new mongoose.Types.ObjectId(),
  name: 'Ian Fleming',
  age: 50
});

author.save(function (err) {
  if (err) return handleError(err);

  const story1 = new Story({
    title: 'Casino Royale',
    author: author._id    // assign the _id from the person
  });

  story1.save(function (err) {
    if (err) return handleError(err);
    // thats it!
  });
});

当你定义的故事和人物之间的关系,很容易操纵它们之间的引用。

在你的情况,你应该定义模型的引用,那么你就能够操纵字段:

Customer.findOne({_id:req.params.customerId}, function(error, customer) {
  if (error) {
    return handleError(error);
  }
  customer.address = newAddress; // set customer's address to the desired address
  // console.log(customer.address.city);
});

检查doc更多。

如何添加模式,以另一个模式阵列?

我有地址模式和客户模式。我有我的客户架构中的一个元素的地址阵列。我将派遣一个ADRESS模型作为我的REQ身体和客户ID REQ PARAM。我怎么能保存到ADRESS它不会忽略客户是内部架构声明数组?

这是我的客户架构

const customerSchema = mongoose.Schema ({
  _id: mongoose.Schema.Types.ObjectId,
  name: String,
  phone_number: String,
  password: String,
  type:{type:String,enum:['admin','user']},
  adresses:['Adress'],
  orders:[{type: mongoose.Schema.Types.ObjectId, ref: 'Order'}]
});

这是我的地址模式

const addressSchema= mongoose.Schema({
    _id:mongoose.Types.ObjectId,
    postalCode:Number,
    city:String,
    district:String,
    neighborhood:String,
    streetNumber:String,
    no:Number,
    buildingName:String,
    apartmentNumber:Number,
    floor:Number,
    coordinates:{
        latitude:Number,
        longitude:Number
    },
    addressName:String,
    customerId: {type: mongoose.Schema.Types.ObjectId,ref:'Customer'}


}); 

我想不出我应该如何去做到这一点。我发现,我会去把我的地址,像这样的客户。

这是我如何获得特定客户

Customer.find({_id:req.params.customerId},(err,data)=>{
    if(err) return next(err);
    else{
      //What I am going to do here?
    }
});

首先,我应该把什么类型的地址阵列是客户架构里面里面?

第二个发现这我要添加地址给客户,我该怎么做之后?猫鼬5.4.11文件是不够的我。此链接似乎我需要的东西,但我不知道如何做到这一点的问题。

.html

回答如下:

好了,基本上就是你寻找的是:关联。您需要建立用户和客户的模型之间的连接。

我们会说,应对例如ID属于用户和用户参考地址对象。

考虑一个例子:

const personSchema = Schema({
  _id: Schema.Types.ObjectId,
  name: String,
  age: Number,
  stories: [{ type: Schema.Types.ObjectId, ref: 'Story' }]
});

const storySchema = Schema({
  author: { type: Schema.Types.ObjectId, ref: 'Person' },
  title: String,
  fans: [{ type: Schema.Types.ObjectId, ref: 'Person' }]
});

const Story = mongoose.model('Story', storySchema);
const Person = mongoose.model('Person', personSchema);

现在,让我们尝试作者分配给特定的创建故事:

const author = new Person({
  _id: new mongoose.Types.ObjectId(),
  name: 'Ian Fleming',
  age: 50
});

author.save(function (err) {
  if (err) return handleError(err);

  const story1 = new Story({
    title: 'Casino Royale',
    author: author._id    // assign the _id from the person
  });

  story1.save(function (err) {
    if (err) return handleError(err);
    // thats it!
  });
});

当你定义的故事和人物之间的关系,很容易操纵它们之间的引用。

在你的情况,你应该定义模型的引用,那么你就能够操纵字段:

Customer.findOne({_id:req.params.customerId}, function(error, customer) {
  if (error) {
    return handleError(error);
  }
  customer.address = newAddress; // set customer's address to the desired address
  // console.log(customer.address.city);
});

检查doc更多。

发布评论

评论列表 (0)

  1. 暂无评论