MongoDB
Introducció
Entorn de treball
Arranca la base de dades:
start-process -NoNewWindow mongod.exeCrea un projecte amb Bun:
mkdir monogdb
cd monogdb
bun init -yAfegeix una dependència amb mongodb:
bun add mongodbObre el projecte amb WebStorm.
Inserir documents
You can insert a document into a collection using the collection.insertOne() method. To insert a
document, define an object that contains the fields and values that you want to store. If the specified
collection does not exist, the insertOne() method creates the collection.
Crea el fitxer fish.ts
A continuació, insereix un peix a la col·lecció fish de la base de dades ocean.
// fish.ts
// MongoDB deployment's connection string
// Create a new client and connect to MongoDB
try
// Connect to the "ocean" database and access its "fish" collection
// Delete all the fish from the collection
await
// Create a document to insert
// Insert the defined document into the "ocean" collection
// Print the ID of the inserted document
`A document was inserted with the _id: `
} finally // Close the MongoDB client connection
await
}Estàs llegint una vista prèvia.
Inicia sessió per llegir l'article complet. Qualsevol compte obre 4 articles gratuïts al mes; l'alumnat i el professorat llegeixen les pàgines del seu curs sense límit.
Inicia sessió