使用nodejs将数据添加到两个不同的mongodb模式中



ownerSchema.js

var ownerSchema = Schema({
fname     : String,
lname     : String,
shopPlace : { 
type: Schema.Types.ObjectId,
ref: 'Shop'
}
});
var Owner = mongoose.model('Owner', ownerSchema);

shopSchema.js

var shopSchema = Schema({
shopName  : String,
location  : String,
startDate : Date,
endDate   : Date
});
var Shop  = mongoose.model('Shop', shopSchema);

这是我的两个不同的模式,我试图在这两个模式中插入数据,但不知道确切的方式,因为我已经写了一个代码来添加数据,但对于单一模式

const Owner = require("../models/ownerSchema");
const addTask = async (req, res) => {
let newOwmer = new Owner(req.body);
try {
newOwner = await newOwner.save();
console.log("Added Successfully");
} catch (err) {
console.log(err);
}
};

在这里,我想知道我如何通过邮递员添加数据我从邮递员发送这样的数据

{
"fname"     : "tom",
"lname"     : "Jerry",
"ShopPlace" : {
"shopName" : "Juice Shop",
"location" : "Mumbai"
}
}

因为它向我显示了这个错误

owner validation failed: Shop: Cast to ObjectId failed for value

如何在mongodb中发送和存储数据?

{
"fname"     : "tom",
"lname"     : "Jerry",
"ShopPlace" : {
"shopName" : "Juice Shop",
"location" : "Mumbai"
}
}

因为这是你的数据,所以你是从邮递员那里寄来的。所以一开始你需要把商店和;此对象的所有者信息。

const ownerData = req.body;             // complete request body
const shopData  = ownerData.ShopPlace;  // pluck only "shopPlace" field from the object
delete ownerData.ShopPlace;             // now, remove the "shopPlace" field from main object (this will leave only 'fname' & 'lname' fields inside the object)

您的代码出现错误,因为商店的ID(即shopPlace)不是在将其添加到所有者集合之前创建。

因此,您必须先添加商店,然后添加所有者因为所有者需要商店id。

const shopPlace = new Shop(shopData);   // This `shopData` is from above step
await shopPlace.save();
ownerData.shopPlace = shopPlace.id;
await Owner.create(ownerData);

这样写控制器:

app.post('/create', async (req, res) => {
const owner = new Owner({
fname: req.body.fname,
lname: req.body.lname,
});
await owner.save();
const shop = new Shop({
shopName: req.body.shopName,
location: req.body.location,
startDate: req.body.startDate,
endDate: req.body.endDate,
});
await shop.save();
const update = await Owner.findByIdAndUpdate(owner._id, {
shopPlace: shop._id,
});
res.send(update);
});

在这里我把数据发送到控制器:

{
"fname": "First Name",
"lname": "Last Name",
"shopName"  : "Shop Name",
"location"  : "location Name",
"startDate" : "2011-10-05T14:48:00.000Z",
"endDate"   : "2011-10-05T14:48:00.000Z"
}

首先需要创建shop,并将生成的id传递给要创建的owner。这里有一个例子(当然仍然需要验证和错误处理):

const shop = new Shop({  
shopName: req.body.ShopPlace.shopName
location: req.body.ShopPlace.location
});    
await shop.save();
const owner = new Owner({
fname: req.body.fname, 
lname: req.body.lname, 
shopPlace: shop._id
});
await owner.save();

它在官方的猫鼬文档中有详细描述:https://mongoosejs.com/docs/populate.html#saving-参考

此外,您可以用另一种方式

app.post('/create', async (req, res) => {
const shop = new Shop({
shopName: req.body.shopName,
location: req.body.location,
startDate: req.body.startDate,
endDate: req.body.endDate,
});
await shop.save();
const owner = new Owner({
fname: req.body.fname,
lname: req.body.lname,
shopPlace: shop._id,
});
await owner.save();
res.send(owner);
});

最新更新