I'm using mongoose to query for one document, and I'd like to pipe it to a stream. My database has multiple account documents. If I simply:
db.Accounts.findOne({}).exec(function(err, data){
...
});
it correctly returns a single document. However,
var mongooseStream = db.Accounts.findOne({}).stream({
transform: JSON.stringify
}),
writeStream = require('fs').createWriteStream('accounts');
mongooseStream.pipe(writeStream);
this newly created writeStream above has every account listed inside of it.
Lastly, if I .find({}).limit(1).stream()
, it will return only one document in the stream.
Any ideas on why streaming findOne({})
isn't working as I expect it to? Thanks!
You have not put any condition on the
findOne()
and moreover you are streaming it. Therefore it is continuously doingfindOne()
and then supplying it to the stream.findOne()
is used as follows :From what i understand you are trying to do is, you are trying to stream only one document of the collection. For that i say,
findOne
is not what you need, becausefindOne
is used to extract exactly one document directly in the form of Javascript object from the database when you define a set of condtions.Read :
Mongoose Queries
db.Collection.findOne()
How to use mongoose findOne