Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Web Technology by (47.6k points)

I have a collection of the following form in MongoDB. As you can see some documends have two members "id" and "xid" and some have only 1 "id" (aside from the Object _id)

{ "id" : 1, }, 

{ "id" : 2, }, 

{ "id" : 3 "xid": 300 } 

]

I want to create a mongoexport statement that exports to a csv only document with id's AND xid's with the value of xid > 0

I currently have the following command:

mongoexport -h host -u user -p pass --db database --collection collections --csv --fields id,xid --query '{"xid":{"$ne":0}}' --out rec.csv

However, this also exports documents that have an id without a xid. So I get something like

xid, id 

12, 3 

,4 

14, 5 

,3 

,2 

12, 5

etc.

Is there a way to export documents that only have both id and xid?

1 Answer

0 votes
by (106k points)

You can use the below-mentioned query for mongoexpert query:-

{ $and: [ 

{xid: {$exists:true}}, 

{xid:{$gt:0}} 

}

Related questions

0 votes
2 answers
asked Oct 16, 2019 in Web Technology by Sammy (47.6k points)
0 votes
2 answers
0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...