如何使用鉴别器、Mongo DB API 和 Mongoose 将数据写入 Cosmos DB?
How do I write data to Cosmos DB using Discriminators, Mongo DB API, and Mongoose?
问题总结
我正在尝试使用 Mongo 数据库 API 和 Mongoose 为我的对象建模将文档写入我的 Cosmos DB。
我希望将我所有的文档集中在一个集合中以降低成本。我想通过使用鉴别器来实现这一点。这是一个 Node.js 项目 v14.4.0,我正在使用 mongodb v3.5.9 和 mongoose v5.9.21。
预期结果
我可以使用上述技术堆栈将文档写入我的 Cosmos DB 中的单个集合。
实际结果
没有数据正在写入数据库。没有错误消息,我正在将 undefined 记录到我的控制台。下一节将对此进行详细介绍。
我试过的
我已按照 Microsoft 文档中有关如何实现我的目标的教程进行操作 https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose
此外,我还看过 Anthony Chu https://anthonychu.ca/post/cosmos-db-mongoose-discriminators/
的博客 post
我在下面包含了我的代码的摘录。
代码
./utils/db.js
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(`mongodb://${process.env.COSMOSDB_HOST}:${process.env.COSMOSDB_PORT}/${process.env.COSMOSDB_DBNAME}?ssl=true&replicaSet=globaldb`, {
auth: {
user: process.env.COSMOSDB_USER,
password: process.env.COSMOSDB_PASSWORD,
},
useNewUrlParser: true,
useUnifiedTopology: true,
retrywrites: false, // Solution
})
.then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));
module.exports = mongoose.connection;
./utils/models/models-discriminator.js
const mongoose = require('mongoose');
const baseConfig = {
discriminatorKey: "_type", //If you've got a lot of different data types, you could also consider setting up a secondary index here.
collection: "alldata" //Name of the Common Collection
};
const commonModel = mongoose.model('Common', new mongoose.Schema({}, baseConfig));
const Family_common = commonModel.discriminator('FamilyType', new mongoose.Schema({
lastName: String,
parents: [{
familyName: String,
firstName: String,
gender: String
}],
children: [{
familyName: String,
firstName: String,
gender: String,
grade: Number
}],
pets:[{
givenName: String
}],
address: {
country: String,
state: String,
city: String
}
}, baseConfig));
const Vacation_common = commonModel.discriminator('VacationDestinationsType', new mongoose.Schema({
name: String,
country: String
}, baseConfig));
module.exports = {
Family_common,
Vacation_common,
}
index.js
const { Family_common, Vacation_common } = require('./utils/models/models-discriminator');
const connection = require('./utils/db');
connection.once('open', () => {
const family_common = new Family_common({
lastName: "Volum",
parents: [
{ firstName: "Thomas" },
{ firstName: "Mary Kay" }
],
children: [
{ firstName: "Ryan", gender: "male", grade: 8 },
{ firstName: "Patrick", gender: "male", grade: 7 }
],
pets: [
{ givenName: "Blackie" }
],
address: { country: "USA", state: "WA", city: "Seattle" }
});
family_common.save((err, saveFamily) => {
console.log("Saved: " + JSON.stringify(saveFamily));
});
const vacay_common = new Vacation_common({
name: "Honolulu",
country: "USA"
});
vacay_common.save((err, saveVacay) => {
console.log("Saved: " + JSON.stringify(saveVacay));
});
});
根据 运行 这段代码,这是输出到我的控制台的内容
成功连接到 CosmosDB
已保存:未定义
已保存:未定义
如果您能提供任何帮助,我将不胜感激。
@Darshitpatel 回答在回调中记录错误消息让我弄清楚为什么这不起作用。我已经编辑了 post 以显示我必须更改才能使其正常工作。
嗨@BradleyGamiMarques,
我已经查看了您的代码,我认为您应该在“vacay_common”的保存回调中记录错误参数。
问题总结
我正在尝试使用 Mongo 数据库 API 和 Mongoose 为我的对象建模将文档写入我的 Cosmos DB。
我希望将我所有的文档集中在一个集合中以降低成本。我想通过使用鉴别器来实现这一点。这是一个 Node.js 项目 v14.4.0,我正在使用 mongodb v3.5.9 和 mongoose v5.9.21。
预期结果
我可以使用上述技术堆栈将文档写入我的 Cosmos DB 中的单个集合。
实际结果
没有数据正在写入数据库。没有错误消息,我正在将 undefined 记录到我的控制台。下一节将对此进行详细介绍。
我试过的
我已按照 Microsoft 文档中有关如何实现我的目标的教程进行操作 https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose
此外,我还看过 Anthony Chu https://anthonychu.ca/post/cosmos-db-mongoose-discriminators/
的博客 post我在下面包含了我的代码的摘录。
代码
./utils/db.js
require('dotenv').config();
const mongoose = require('mongoose');
mongoose.connect(`mongodb://${process.env.COSMOSDB_HOST}:${process.env.COSMOSDB_PORT}/${process.env.COSMOSDB_DBNAME}?ssl=true&replicaSet=globaldb`, {
auth: {
user: process.env.COSMOSDB_USER,
password: process.env.COSMOSDB_PASSWORD,
},
useNewUrlParser: true,
useUnifiedTopology: true,
retrywrites: false, // Solution
})
.then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));
module.exports = mongoose.connection;
./utils/models/models-discriminator.js
const mongoose = require('mongoose');
const baseConfig = {
discriminatorKey: "_type", //If you've got a lot of different data types, you could also consider setting up a secondary index here.
collection: "alldata" //Name of the Common Collection
};
const commonModel = mongoose.model('Common', new mongoose.Schema({}, baseConfig));
const Family_common = commonModel.discriminator('FamilyType', new mongoose.Schema({
lastName: String,
parents: [{
familyName: String,
firstName: String,
gender: String
}],
children: [{
familyName: String,
firstName: String,
gender: String,
grade: Number
}],
pets:[{
givenName: String
}],
address: {
country: String,
state: String,
city: String
}
}, baseConfig));
const Vacation_common = commonModel.discriminator('VacationDestinationsType', new mongoose.Schema({
name: String,
country: String
}, baseConfig));
module.exports = {
Family_common,
Vacation_common,
}
index.js
const { Family_common, Vacation_common } = require('./utils/models/models-discriminator');
const connection = require('./utils/db');
connection.once('open', () => {
const family_common = new Family_common({
lastName: "Volum",
parents: [
{ firstName: "Thomas" },
{ firstName: "Mary Kay" }
],
children: [
{ firstName: "Ryan", gender: "male", grade: 8 },
{ firstName: "Patrick", gender: "male", grade: 7 }
],
pets: [
{ givenName: "Blackie" }
],
address: { country: "USA", state: "WA", city: "Seattle" }
});
family_common.save((err, saveFamily) => {
console.log("Saved: " + JSON.stringify(saveFamily));
});
const vacay_common = new Vacation_common({
name: "Honolulu",
country: "USA"
});
vacay_common.save((err, saveVacay) => {
console.log("Saved: " + JSON.stringify(saveVacay));
});
});
根据 运行 这段代码,这是输出到我的控制台的内容
成功连接到 CosmosDB
已保存:未定义
已保存:未定义
如果您能提供任何帮助,我将不胜感激。
@Darshitpatel 回答在回调中记录错误消息让我弄清楚为什么这不起作用。我已经编辑了 post 以显示我必须更改才能使其正常工作。
嗨@BradleyGamiMarques,
我已经查看了您的代码,我认为您应该在“vacay_common”的保存回调中记录错误参数。