I have an aggregation pipeline that headhunts for doublets in my database and I need to print the cursor on a text file. I’m working from the terminal and I have ubuntu on my machine
Can anyone help me with this?
This is my aggregation : db.candidates.aggregate([{$group:{_id:{name:"$name",xing:"$xing"},count:{$sum:1},docs:{$push:"$_id"}}},{$match:{count:{$gt:1}}}])
Hey @Anna_Henningsen !
Amazing!
That`s mean that I can pass the cursor as a variable in mongosh?
And in the last line, I just need to swap /tmp/out with a path to the designated file right?
@Pietro_Simcic Yes, you can store cursors in variables. If you need the results as arrays, you can also call .toArray() on them and store that in a variable (however, streaming will work much better when you’re dealing with large datasets).
And yup, just replace it with the filename you want it to have
@Pietro_Simcic Can you wrap that whole piece of code in { ... } curly brackets? If you paste it line-by-line into the shell, it won’t work because mongosh tries to print the cursor output after the first line, before it has even seen that there is more code.
@Anna_Henningsen the result is an empty file.
I also tried to add an extension but I have the same result.
If needed I can send you the WriteStream object.
Does the single line you are looking at contain all the duplicates (i.e. are these all duplicates of a single name/xing combination) or are you looking at multiple groups?
What happens when you add cusorAsStream.pipe(process.stderr); before the other .pipe() command (in the same {...} block)? Are you seeing more results then?
Also: Are you running a recent mongosh version, and does the aggregation take a long time to run? (The command returns control to the user immediately, but it may take a bit of time for the aggregation to actually run and becomes written to disk.)
yes, I’m looking at the right file, there is nothing else in the folder :).
This is the content of the file: {"_id":{"name":"A","xing":"X"},"count":2,"docs":[{"$oid":"61377252a41e6aebfbd98cae"},{"$oid":"61377252a41e6aebfbd98caf"}]}
3.when I add the cusorAsStream.pipe(process.stderr); in this way :
@steevej No – in the shell, async/await is implicit, and in both the shell and modern versions of the Node.js driver, db.test.aggregate() returns a Cursor object, not a Promise.