You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongoose findOne等查询方法无返回结果问题求助

Troubleshooting Empty Mongoose Queries (findOne/find) on MongoDB 3.6

Hey there, I know how frustrating it is when even your stripped-down test code won’t return results—let’s break down the most likely culprits and fixes for your setup (Mongoose 3.8.12, MongoDB 3.6.3, Node.js 8.10.0, Ubuntu 16.04):

1. First, Confirm Your MongoDB Connection Is Actually Working

Older Mongoose versions (like 3.8.x) don’t make connection errors obvious by default. If your app isn’t properly connected, all queries will silently return nothing. Add error handling to your connect call to verify:

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test', function(err) {
  if (err) {
    console.error('Critical connection error:', err);
    return;
  }
  console.log('Successfully connected to the test database');
});

// Also listen for ongoing connection issues
mongoose.connection.on('error', console.error.bind(console, 'Connection error:'));

If you see an error here, that’s the root of your problem—move to the next step.

2. Fix Mongoose-MongoDB Compatibility Issues

Mongoose 3.8.12 is a 2014 release, while MongoDB 3.6 came out in 2017—they have major compatibility gaps, especially around authentication. MongoDB 3.6 defaults to the SCRAM-SHA-256 auth mechanism, which Mongoose 3.x doesn’t support. You have two options:

  • Upgrade Mongoose (Recommended): Upgrade to at least Mongoose 4.11.x (the first version to officially support MongoDB 3.6). Run npm install mongoose@4.11.15 --save (a stable 4.x release) and retry your queries.
  • Force Old Auth Mechanism: If you can’t upgrade Mongoose, add authMechanism=SCRAM-SHA-1 to your connection string:
    mongoose.connect('mongodb://localhost/test?authMechanism=SCRAM-SHA-1');
    

3. Verify Your Model Matches the Database Collection

Mongoose automatically pluralizes and lowercases your model name to map to a collection. So a Cat model looks for a cats collection by default. If your data is in a differently named collection (like cat), explicitly specify it in your model definition:

const catSchema = new mongoose.Schema({ name: String });
// Third argument is the exact collection name in MongoDB
const Cat = mongoose.model('Cat', catSchema, 'cat');

Also double-check that your schema matches the structure of the data in your database—if a field you’re querying isn’t defined in the schema (and strict: true is enabled, which is default), Mongoose might behave unexpectedly, though it shouldn’t prevent the query from finding documents.

4. Test Raw Queries in the MongoDB Shell

Rule out Mongoose entirely by querying directly in the MongoDB shell:

  1. Run mongo to open the shell
  2. Switch to your test database: use test
  3. Run db.cats.find() (or your actual collection name)
    If no results show up here, your data isn’t stored in the expected database/collection—fix that first. If results do show up, the issue is definitely with Mongoose.

5. Ensure You’re Handling Asynchronous Queries Correctly

Mongoose queries are asynchronous, so if your test code exits before the query completes, you’ll never see results. Make sure you’re using callbacks or promises to wait for the response:

// With callback
Cat.findOne({ name: 'Garfield' }, function(err, cat) {
  if (err) {
    console.error('Query error:', err);
    return;
  }
  console.log('Found cat:', cat);
});

// With promises (Mongoose 3.8 supports promises, though you may need to enable them)
Cat.findOne({ name: 'Garfield' })
  .then(cat => console.log('Found cat:', cat))
  .catch(err => console.error('Query error:', err));

If you’re running a script, add a delay or use process.exit() only after the query completes, otherwise Node.js will exit immediately.

6. Check MongoDB User Permissions

If you’ve enabled authentication on your MongoDB instance, ensure the user in your connection string has read access to the test database. Run this in the MongoDB shell to verify:

use admin
db.grantRolesToUser('yourUsername', [{ role: 'read', db: 'test' }])

The most likely fix here is upgrading Mongoose to a version compatible with MongoDB 3.6—old Mongoose versions just weren’t built to work with newer MongoDB releases.

内容的提问来源于stack exchange,提问作者Craig Zych

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:10:56