question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

findOne returns random value but collection.findOne does not!

See original GitHub issue

Do you want to request a feature or report a bug? Bug

What is the current behavior? returns random value from findOne. I am pretty sure its not suppose too or maybe I am doing something wrong? In older version it worked as expected.

If the current behavior is a bug, please provide the steps to reproduce.

const mongoose = require('./db').mongoose;

function pageViewsSchema() {


  const schema = {
    pageId: {type: mongoose.Schema.Types.ObjectId},
  };

  const collectionName = 'Pageviews';
  const mongoSchema = mongoose.Schema(schema);

  const Pageviews = mongoose.model(collectionName, mongoSchema);

  return Pageviews;
}

module.exports = pageViewsSchema();
const Pageviews = require('./pageviews');

const awaitPageview = await Pageviews.findOne({'asdasda': 'adadasdsa'});


What is the expected behavior? it should return null. When i use Pageviews.collection.findOne returns null as expected.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that “latest” is not a version. node: v15.9.0 mongodb: 5.0.2 mongoose: 6.0.7

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
saveman71commented, Sep 24, 2021

This is because asdasda isn’t in you schema, so it’s stripped out, resulting in an empty query {}, querying the first document it finds.

0reactions
xoxoxocommented, Sep 24, 2021

cheers just posted there will close this one

Read more comments on GitHub >

github_iconTop Results From Across the Web

Mongoose findOne() and find() return invalid values, when ...
It seems to be some random document fetched from the Currencies collection, even though none of the documents in the collection, including the ......
Read more >
db.collection.findOne() — MongoDB Manual
Returns one document that satisfies the specified query criteria on the collection or view. If multiple documents satisfy the query, this method returns...
Read more >
Mongoose v6.8.2: Queries
What results is depends on the operation: For findOne() it is a potentially-null single document, find() a list of documents, count() the number...
Read more >
4. Querying - MongoDB: The Definitive Guide, 2nd Edition [Book]
Sometimes you do not need all of the key/value pairs in a document returned. If this is the case, you can pass a...
Read more >
[SOLVED] Find random entry without fetching entire mongo ...
My goal is to fetch a random entry in my collection, one entry at a ... pick a random value 1 - Max...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found