如何使用鉴别器,Mongo DB API和Mongoose将数据写入Cosmos DB?

布拉德利·加米·马克斯

问题总结

我试图使用Mongo DB API和Mongoose进行对象建模,将文档写入Cosmos DB。

我希望将所有文件都放在一个集合中,以降低成本。我想通过使用鉴别器来实现。这是一个Node.js项目v14.4.0,我正在使用mongodb v3.5.9和mongoose v5.9.21。

预期成绩

我可以使用上述技术堆栈将文档写入Cosmos DB中的单个集合。

实际结果

没有数据被写入数据库。没有错误消息,并且我的控制台未登录。下一节将对此进行更多介绍。

我尝试过的

我已经按照Microsoft文档https://docs.microsoft.com/en-us/azure/cosmos-db/mongodb-mongoose上有关如何实现目标的教程进行了学习

此外,我看过来自Anthony Chu的博客文章https://anthonychu.ca/post/cosmos-db-mongoose-discriminators/

我在下面提供了我的代码的摘录。

./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的回答在回调中记录错误消息让我弄清楚了为什么它不起作用。我已经编辑了帖子,以显示为了使此功能起作用我必须进行的更改。

达希尔帕特尔

嗨@BradleyGamiMarques,

我已经检查了您的代码,我认为您应该在vacay_common的save回调中记录err参数。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章