azure cosmos db已在mongodb.com中创建了集合
当我使用mean stack练习一些基本的crud操作时,我创建了一个名为“test”的数据库和“videos”集合,后来我开始学习Cosmos DB,其中创建了数据库“Cosmos DB demo”,集合为“video collection”。我可以将我的应用程序连接到cosmos DB,但我惊讶地看到,当我执行post操作时,数据被插入到测试>集合中。。。而不是在cosmos db demo>视频采集中。这个数据库是如何插入的? 该记录不应该插入到我连接的记录中吗azure cosmos db已在mongodb.com中创建了集合,mongodb,mongoose,azure-cosmosdb,Mongodb,Mongoose,Azure Cosmosdb,当我使用mean stack练习一些基本的crud操作时,我创建了一个名为“test”的数据库和“videos”集合,后来我开始学习Cosmos DB,其中创建了数据库“Cosmos DB demo”,集合为“video collection”。我可以将我的应用程序连接到cosmos DB,但我惊讶地看到,当我执行post操作时,数据被插入到测试>集合中。。。而不是在cosmos db demo>视频采集中。这个数据库是如何插入的? 该记录不应该插入到我连接的记录中吗 猫鼬mongodb://c
猫鼬mongodb://cosmos-db-acc:....@cosmos-db-acc.mongo.cosmos.azure.com:10255/?ssl=true&replicset=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@cosmos-db-acc@')。然后(()=>{console.log('connected to cosmos-db');})。catch((err)=>{console.log(err)}) 关于这个问题,我们无法在应用程序中定义数据库名称和连接名称,以告诉应用程序连接哪个数据库和连接。关于如何实现它,请在连接字符串中添加db名称,并在模型中添加
{collection:”“}
比如说
.env
文件您能提供您的代码吗?添加了连接代码。请将连接字符串更新为
mongodb://cosmos-db-acc:....@cosmos-db-acc.mongo.cosmos.azure.com:10255/
。它将告诉应用程序连接哪个数据库。将集合名称和数据库名称添加到连接字符串中效果很好。非常感谢。
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
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));
});