diff --git a/getting-started/nodejs-setup.md b/getting-started/nodejs-setup.md index 905781f..1a91fd5 100644 --- a/getting-started/nodejs-setup.md +++ b/getting-started/nodejs-setup.md @@ -62,48 +62,51 @@ const { MongoClient } = require('mongodb'); const uri = 'mongodb://:@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true'; const client = new MongoClient(uri); -async function connect() { - try { - await client.connect(); - const db = client.db('your_database'); - return db; - } catch (error) { - console.error('Connection error:', error); - throw error; - } +async function main() { + await client.connect(); + const db = client.db('your_database'); + console.log('connected'); + return db; } + +main().catch((error) => { + console.error('Connection error:', error); + process.exit(1); +}); ``` ## Basic Operations -1. Creating collections - ```javascript - const collection = db.collection('your_collection'); - ``` - -2. Document operations +The operations below all run inside `main()`, after `const db = client.db(...)` above. +`await` is only valid inside an `async` function, and `db` only exists in that scope — +running these at the top level of a file gives `ReferenceError: db is not defined`. - ```javascript - const users = db.collection('users'); +```javascript +async function main() { + await client.connect(); + const db = client.db('your_database'); + const users = db.collection('users'); - await users.insertOne({ name: 'John Doe', email: 'john@example.com', createdAt: new Date() }); - await users.insertMany([ - { name: 'Jane Smith', email: 'jane@example.com' }, - { name: 'Bob Johnson', email: 'bob@example.com' }, - ]); + await users.insertOne({ name: 'John Doe', email: 'john@example.com', createdAt: new Date() }); + await users.insertMany([ + { name: 'Jane Smith', email: 'jane@example.com' }, + { name: 'Bob Johnson', email: 'bob@example.com' }, + ]); - await users.updateOne({ name: 'John Doe' }, { $set: { status: 'active' } }); - console.log(await users.findOne({ name: 'John Doe' })); - console.log(await users.countDocuments()); + await users.updateOne({ name: 'John Doe' }, { $set: { status: 'active' } }); + console.log(await users.findOne({ name: 'John Doe' })); + console.log(await users.countDocuments()); - await users.deleteOne({ name: 'Bob Johnson' }); - ``` + await users.deleteOne({ name: 'Bob Johnson' }); - Always close the client when the process is done: + await client.close(); +} - ```javascript - await client.close(); - ``` +main().catch((error) => { + console.error(error); + process.exit(1); +}); +``` ## Beyond CRUD diff --git a/getting-started/python-setup.md b/getting-started/python-setup.md index f9eb5da..2515211 100644 --- a/getting-started/python-setup.md +++ b/getting-started/python-setup.md @@ -185,7 +185,32 @@ DocumentDB Local accepts TLS connections on the gateway port and requires authen 3. Vector search `$vectorSearch` is an aggregation stage and must be the first stage in the - pipeline — it does not work inside `find()`. + pipeline — it does not work inside `find()`. It also requires a vector index + on the field, or the query fails with + `Similarity index was not found for a vector similarity search query`. + + Create the index once: + + ```python + db.command({ + 'createIndexes': 'your_collection', + 'indexes': [ + { + 'name': 'embeddings_idx', + 'key': {'embeddings': 'cosmosSearch'}, + 'cosmosSearchOptions': { + 'kind': 'vector-ivf', + 'numLists': 100, + 'similarity': 'COS', + 'dimensions': 3 + } + } + ] + }) + ``` + + `dimensions` must match the length of the vectors you store and query. Three keeps + the example short; real embeddings are typically 384, 768, or 1536 wide. ```python results = collection.aggregate([ diff --git a/postgres-api/index.md b/postgres-api/index.md index 4b526e1..758e488 100644 --- a/postgres-api/index.md +++ b/postgres-api/index.md @@ -1,11 +1,11 @@ --- title: Components -description: Learn about pg_documentdb_core, pg_documentdb, and pg_documentdb_gw — the PostgreSQL extensions that enable BSON support, document operations, and MongoDB wire protocol compatibility in Postgres. +description: Learn about pg_documentdb_core, pg_documentdb, and pg_documentdb_gw — the two PostgreSQL extensions and the gateway that enable BSON support, document operations, and MongoDB wire protocol compatibility in Postgres. --- # Components -The DocumentDB implementation consists of three PostgreSQL extensions that work together to provide MongoDB-compatible document database functionality on top of PostgreSQL. +The DocumentDB implementation consists of two PostgreSQL extensions and a gateway that work together to provide MongoDB-compatible document database functionality on top of PostgreSQL. ## pg_documentdb_core