import { MongoClient } from "mongodb";
// Replace the uri string with your MongoDB deployment's connection string.
const uri = "<connection string uri>";
const client = new MongoClient(uri);
async function run() {
try {
// Get the database and collection on which to run the operation
const database = client.db("sample_mflix");
const movies = database.collection("movies");
// Query for movies that have a runtime less than 15 minutes
const query = { runtime: { $lt: 15 } };
const options = {
// Sort returned documents in ascending order by title (A->Z)
sort: { title: 1 },
// Include only the `title` and `imdb` fields in each returned document
projection: { _id: 0, title: 1, imdb: 1 },
};
// Execute query
const cursor = movies.find(query, options);
// Print a message if no documents were found
if ((await movies.countDocuments(query)) === 0) {
console.log("No documents found!");
}
// Print returned documents
for await (const doc of cursor) {
console.dir(doc);
}
} finally {
await client.close();
}
}
run().catch(console.dir);
Mongodb+nodejs :Find Multiple Document
最新推荐文章于 2024-08-02 22:54:22 发布