mongodb-core是否支持db.getCollection('abcd.data').count()等命令及执行方法
db.getCollection('abcd.data').count()-style commands? Hey there! Let's clear this up for you quickly:
First off, db.getCollection('abcd.data').count() is a MongoDB Shell-specific syntax, not a raw MongoDB wire protocol command. The mongodb-core library is a low-level driver that interacts directly with MongoDB's native command protocol, so you can't pass Shell commands directly to its command() method like you tried in your code.
How to run a count operation with mongodb-core
Instead of using Shell syntax, you need to send the raw MongoDB command document that the Shell translates to. For counting documents, here's what you need to know:
- The legacy
countcommand is still supported but deprecated (MongoDB 4.0+ recommends usingcountDocumentsorestimatedDocumentCountinstead) - You'll need to target the correct database and collection in your command parameters
Let's fix your test code with a working example:
const { Server, ReadPreference } = require('mongodb-core'); // Initialize the server connection const server = new Server({ host: 'localhost', port: 27017 }); server.on('connect', (conn) => { // Raw count command for the "abcd.data" collection in the target database // Replace "your-db-name" with the actual database name holding the collection const countCommand = { count: 'abcd.data', // Name of your collection query: {} // Optional: add a query filter if you don't want all documents }; // Execute the command against the target database conn.command('your-db-name', countCommand, { readPreference: new ReadPreference('secondary') }, (err, result) => { if (err) { console.error('Error executing count:', err); } else { console.log('Count result:', result); // Example output: { n: 42, ok: 1 } where n is the document count } // Clean up the connection conn.destroy(); server.destroy(); }); }); // Initiate the connection server.connect();
Key fixes from your original code:
- We're passing a command document (
countCommand) instead of a Shell command string - The
command()method's first parameter is the database name (your original code had the command string here, which was incorrect) - Properly imported
ReadPreferencefrommongodb-core - Fixed connection cleanup logic to use the connection object returned in the
connectevent
Why you didn't find this in the docs
Since mongodb-core is a low-level driver, its documentation focuses on the wire protocol commands rather than the Shell's syntactic sugar. The Shell commands like db.getCollection().count() are just wrappers around these raw protocol commands, which is why you won't see direct references to Shell syntax in the mongodb-core docs.
If you're working with modern MongoDB versions (4.0+), consider using the countDocuments command instead for accurate counts (the legacy count has some edge cases with sharded clusters):
const countDocsCommand = { countDocuments: 'abcd.data', filter: {} // Optional filter }; // Pass this to conn.command() the same way as above
内容的提问来源于stack exchange,提问作者Aero Wang

