findOne returns random value but collection.findOne does not!
See original GitHub issueDo 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.cheers just posted there will close this one