sonuD
sonuD

Reputation: 57

azure cosmos db has collections created in mongodb.com

while I was practicing some basic crud operations using mean stack, I had created a database named "test" and inside "videos" collection, and later I started to learn Cosmos DB where created the database "cosmos-db-demo" and collection as "video-collection".I am able to connect my app to cosmos DB but what I am surprised to see is when I am performing post-operation, the data is getting inserted into test>collections... and not in cosmos-db-demo>video-collection. how did this DB got inserted? shouldn't the record be inserted to the one I connected?

mongoose.connect('mongodb://cosmos-db-acc:[email protected]:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@cosmos-db-acc@').then(() => { console.log('connected to cosmos DB');}).catch((err) => {console.log(err)});

Upvotes: 1

Views: 564

Answers (1)

Jim Xu
Jim Xu

Reputation: 23111

Regarding the issue, we cann define the database name and connection name in the application to tell the application to connect which db and connection. Regarding how to implement it, add the db name in your connection string and add the { collection: "<connection name>" } in your model

For example

  1. Create .env file
COSMODDB_USER = "<Azure Cosmos account's user name, usually the database account name>"
COSMOSDB_PASSWORD = "<Azure Cosmos account password, this is one of the keys specified in your account>"
COSMOSDB_HOST= "<Azure Cosmos Host name>"
COSMOSDB_PORT=10255
  1. Code
var mongoose = require("mongoose");
var env = require("dotenv").config();

//connect
mongoose
  .connect(
    "mongodb://" +
      process.env.COSMOSDB_HOST +
      ":" +
      process.env.COSMOSDB_PORT +
      "/" +
      "User" + //  tell the application to connect which databse
      "?ssl=true&replicaSet=globaldb",
    {
      auth: {
        user: process.env.COSMODDB_USER,
        password: process.env.COSMOSDB_PASSWORD,
      },
      useNewUrlParser: true,
      useUnifiedTopology: true,
      retryWrites: false,
    },
  )
  .then(() => console.log("Connection to CosmosDB successful"))
  .catch((err) => console.error(err));

// define model
const Family = mongoose.model(
  "Family",
  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,
      },
    },
    { collection: "Familay" }, // tell the application to connect which connection
  ),
);

const family = new Family({
  lastName: "Volum",
  parents: [{ firstName: "Thomas" }, { firstName: "Mary Kay" }],
  children: [
    { firstName: "Ryan", gender: "male", grade: 8 },
    { firstName: "Patrick", gender: "male", grade: 7 },
  ],
  pets: [{ givenName: "Buddy" }],
  address: { country: "USA", state: "WA", city: "Seattle" },
});

family.save((err, saveFamily) => {
  console.log(JSON.stringify(saveFamily));
});

enter image description here enter image description here

Upvotes: 2

Related Questions