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 Visual Studio Code.
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ó amb Google per llegir la pàgina completa.
Inicia sessió amb GoogleAmb qualsevol compte de Google. Només et demanarem que acceptis les condicions del servei.