I try to use ‘mongoexport’ to retrieve the ‘Url’ value:
$ mongoexport -d "db" -c "collection" -q '{"Id":"5400490185"} | jq .
return:
{
"_id": {
"$oid": "641e8845c0a3fde195b5901b"
},
"Url": "https://example.org",
"Id": "5400490185",
"Title": "xxx",
"Description": "foobar",
"Date_Time": "1679697365"
}
Now I try to fetch only ‘Url’, from the doc https://www.mongodb.com/docs/manual/reference/sql-comparison/ should be
db.people.find(
{ status: "A" },
{ user_id: 1, status: 1, _id: 0 }
)
so I try:
$ mongoexport -d "db" -c "collection" -q '{"Id":"5400490185"}, {"Url": "1", _id: "0"}'
but I get
invalid character ',' after top-level value
What’s wrong?
Tried also:
$ mongoexport -d "db" -c "collection" -q '{"Id":"5400490185"}' -f 'Url'
But doesn’t produce what’s expected
3
Answers
Found a workaround:
But I guess there's a better way to do it without a pipe.
You could also use the
mongosh
:If it does not work, try
db.people.find(...).toArray().forEach(...)
.As a one-liner
It’s far easier in Python and shell here-doc (‘mongodb’ JS query syntax is awful IMHO):
Usage:
Simple, clear, efficient.