Show Mongodb query projection in the Log - mongodb

(updated)
I want to see the field restriction (projection) used in a query in the log, not just the query itself, so I can see exactly what's being requested. I've set 'vvvv=true' along with 'verbose=true' in the config file, so given a shell query;
db.col.find({},{Name:1}).count()
I can then see this in the log;
command: {"count":"col, query:{_id:23}, fields: {Name: 1.0}}}
However, the following query does NOT.
db.col.find({_id:23},{Name:1})
Nor am I seeing this through the C# driver when I use Fields.Include on the MongoCursor.
What am I missing?

The MongoD server currently does not log the projection. There is a ticket in our JIRA for this: https://jira.mongodb.org/browse/SERVER-3129 — please vote for it if you want to show this is important to you.

Related

MongoDB Collection export error Path collision at tokens

I am trying to export my collections from Atlas to my Local MongoDB with MongoDB Compass tool. The issue is I can export some collections but my user collection returning error.
Error is: Path collision at tokens.0BUo4-zWowM9ldTIUbs57Q remaining portion 0BUo4-zWowM9ldTIUbs57Q
Any help appreciated, thanks.
MongoDB Compass version is 1.28.1
This error occurs because there are two different types. Probably you have your token set as an object, or as an empty string.
The solution to use Mongo Compass is manually unchecking the "Root level field" that marks as a string. Check the following example:
The error occurs because the field "Fotos_da_Ocorrência" can be an empty string "" or and object {bucketName: "", comment: "", elementId: ""}.
When i uncheck the Root Level Field it exports normally.
By some search, I saw some other people facing similar problem. So it may be caused by some bug on MongoDB Compass version is 1.28.1. Workaround solution was using command line interface like below.
mongodump --uri="<connection-string>" --out /path/to/export

MONGODB: $in operator not matching any record

community!
I am in a weird situation. The direct equally check returns result, but when using $in I am not getting any records.
db.getCollection("voter").find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
db.voter.find({"id":{$in:["db1eefc5-09ad-4d4f-a31a-db63d8261913"]}})
Doesn't return anything.
db.voter.find({id: "db1eefc5-09ad-4d4f-a31a-db63d8261913"})
Returns the desired record.
Being more of a fullstack developer, I don't know what's happening in-depth, but I am sure that both things shall work ideally which is not the case here.
Extra info:
I have defined hashed unique indexes on id.
Thanks.
The problem is pretty simple:
On the first screen you're running your query against admin database
while second query gets executed against crmadmin db

Pymongo ignoring my limit parameter

I am using Pymongo (v3.5.1) in a Python v3.6.3 Jupyter notebook.
Problem
Even-though I am limiting my results, the db.collection.find() is still retrieving all results before returning
My code:
for post in posts.find({'subreddit_1':"the_donald"}, limit=2):
print(post)
exit
Background
I have imported the Reddit comment data set (RC_2017-01) from files.pushshift.io and created an index on the subreddit field (subreddit_1).
My Indexes
I believe this is caused by the collection having no index on your query term, as exhibited by the line:
planSummary: COLLSCAN
which means that to answer your query, MongoDB is forced to look at each document in the collection one by one.
Creating an index to support your query should help. You can create an index in the mongo shell by executing:
db.posts.createIndex({'subreddit_1': 1})
This is assuming your collection is named posts.
Please note that creating that index would only help with the query you posted. It's likely that different index would be needed for different type of queries.
To read more about how indexing works in MongoDB, check out https://docs.mongodb.com/manual/indexes/
I think you need to change the query, because in find() method 2nd parameter is projection. Find() always return cursor and limit function always works on cursor.
So the syntax should like below:
for post in posts.find({'subreddit_1':"the_donald"})[<start_index>:<end_index>]
print(post)
exit
OR
for post in posts.find({'subreddit_1':"the_donald"}).limit(2)
print(post)
exit
Please read the doc for detail

Is there a better way to export a mongodb query to a new collection?

What I want:
I have a master collection of products, I then want to filter them and put them in a separate collection.
db.masterproducts.find({category:"scuba gear"}).copyTo(db.newcollection)
Of course, I realise the 'copyTo' does not exist.
I thought I could do it with MapReduce as results are created in a new collection using the new 'out' parameter in v1.8; however this new collection is not a subset of my original collection. Or can it be if I use MapReduce correctly?
To get around it I am currently doing this:
Step 1:
/usr/local/mongodb/bin/mongodump --db database --collection masterproducts -q '{category:"scuba gear"}'
Step 2:
/usr/local/mongodb/bin/mongorestore -d database -c newcollection --drop packages.bson
My 2 step method just seems rather inefficient!
Any help greatly appreciated.
Thanks
Bob
You can iterate through your query result and save each item like this:
db.oldCollection.find(query).forEach(function(x){db.newCollection.save(x);})
You can create small server side javascript (like this one, just add filtering you want) and execute it using eval
You can use dump/restore in the way you described above
Copy collection command shoud be in mongodb soon (will be done in votes order)! See jira feature.
You should be able to create a subset with mapreduce (using 'out'). The problem is mapreduce has a special output format so your documents are going to be transformed (there is a JIRA ticket to add support for another format, but I can not find it at the moment). It is also going to be very inefficent :/
Copying a cursor to a collection makes a lot of sense, I suggest creating a ticket for this.
there is also toArray() method which can be used:
//create new collection
db.creatCollection("resultCollection")
// now query for type="foo" and insert the results into new collection
db.resultCollection.insert( (db.orginialCollection.find({type:'foo'}).toArray())

MongoDB - mongohub gui - queries by _id objectid type

I am using MongoHub GUI for MongoDB: http://mongohub.todayclose.com/
I would like to be able to query by ObjectId as this is what MongoHub is returing for _id. How to do this, something like {"_id":"4d1b4687a6d5437619000000"} is not working??
cheers,
/Marcin
try following code:
{"_id": ObjectId("4d1b4687a6d5437619000000")}
check this for more details
It looks as if MongoHub is broken in the case of supplying a function in a query (ObjectId, as galimy rightly suggested). If you enter the query as galimy suggested, then copy and paste the full query that MongoHub says it's going to execute (grayed out above the Query text input) into a connected mongo CLI console, it works fine.
I would recommend learning to use the mongo console -- I've found two bugs in 5 minutes of playing with MongoHub, and when you're typing in JSON for your queries anyway the GUI is doing very little for you.
OK, it has been fixed in recent MongoHub release. Cheers
{"_id": { $oid: "4d1b4687a6d5437619000000"}} definitely should work. Java MongoDB driver implicitly creates ObjectId object in the object has '$oid' property.
Also all the same is for date using '$date' property.