Delete an order from Mongo instance running on AWS - mongodb

There is an Mongo DB instance running on AWS so it is can be accessed only from terminal (I guess).
the table is called orders
to see what entries are in the table I'm using this command:
db.orders.find()
it returns a list of all orders. The problem comes when I want to delete one order. If entering in terminal db.orders. and than hit the tab it returns the list of all available commands.
One that I need I think it's db.orders.deleteOne() but I don't know what argument to send to it.
The order that must be deleted looks like this:
{ "_id" : ObjectId("5ea1a1c48a0c055870168770"), "display_id" : "WOW" }
so I tried with in many ways like:
db.orders.deleteOne(5ea1a1c48a0c055870168770)
db.orders.deleteOne("5ea1a1c48a0c055870168770")
db.orders.deleteOne(_id: 5ea1a1c48a0c055870168770)
db.orders.deleteOne("_id": "5ea1a1c48a0c055870168770")
but none of the seem to work.

db.orders.deleteOne( { "_id" : ObjectId("563237a41a4d68582c2509da") } );

Related

MongoDB does not return a field

It must be a silly mistake but I can't find it.
When I run db.getCollection('communes').findOne({}),
I obtain:
{
"_id" : ObjectId("59b851a19db72301ae771c57"),
"COMMUNE" : "ALAA6",
"LIBGEO" : "ROHRBACH",
"PAYS" : "Allemagne"
}
which is fine.
But when I run db.getCollection('communes').findOne({COMMUNE: "ALAA6"}), it returns nothing!
For strange reasons, filtering on other fields work, so when I run db.getCollection('communes').findOne({LIBGEO: "ROHRBACH"}), it returns the result. Same thing filtering on "PAYS".
Adding quotes around COMMUNE, i.e. running db.getCollection('communes').findOne({"COMMUNE": "ALAA6"}) or using find instead of findOne doesn't change anything.
Any idea?
Ok, my fault. The collection has been created from an Excel export, and it seems the first column contains a strange character. Querying from the Robo3T client did not show it, but querying from Python returns:
{'LIBGEO': 'ROHRBACH',
'PAYS': 'Allemagne',
'_id': ObjectId('59b851a19db72301ae771c57'),
'\ufeffCOMMUNE': 'ALAA6'}
so obviously the \ufeffchar before COMMUNEshould be removed...

Mongodb: How to drop a collection named "stats"?

I created a collection named "stats" but whenever I try to delete it by using db.stats.drop() cmd line. It shows an error.
TypeError: db.stats.drop is not a function
Even tried this.
db["stats"].drop
Any solution?
if the collection exists db.stats.drop() should work (depending on the mongodb version maybe) or else perhaps db.getCollection("stats").drop();
but if all else fails, run: db.runCommand({drop : "stats"}) this should drop it if it exists without any error at least.

Mongodb cannot use the part (...) to traverse the element ({...: undefined})]

After upgrade to 3.0 mongo driver i am receiving some new error on update request. For update like this:
db.table.update({_id: .... } , {$set : { "tags.Tag1" : true }});
I am receiving
cannot use the part (tags of tags.Tag1) to traverse the element ({tags: null})]]
The problem is that my updated document already contains default value for tags : null. If I manually remove it from document , update starts to work correctly. It is some new behavior for me , and it is happens after updating mongo driver from 2 to 3 ( not even database itself).
But now I wonder now how to avoid this error. I can of course check if "tags" already defined and only then make $set to element or the whole map. But it means 3 requests vs one old and the other problems like atomicity.
Although it's an old post but I think what you are looking for is the $ positional operator
I am guessing your "tags" is an array. So the above example could be something like
db.table.update({_id: .... } , {$set : { "tags.$.Tag1" : true }});
Hope it helps!
Yes, it can be updated... I had resolved similar problem
Resolved this
db.table.updateById({_id: .... } , {$set : { "levelSpecificData.scale.uom": "feet"}});
5b1f566ed65c7dcc34aaa7d5 MongoError: cannot use the part (scale of levelSpecificData.scale.uom) to traverse the element ({scale: false})
where in my collection 'levelSpecificData.scale' was a Boolean type T/F
I changed the default value type of levelSpecificData.scale to '{}' empty object... Surprisingly it worked fine after changing default values to object, since I want to treat scale as an object reference this solution was all good for it.

Mongodb - query with '$or' gives no results

There is extremely weird thing happening on my database. I have a query:
db.Tag.find({"word":"foo"})
this thing matches one object. it's nice.
Now, there's second query
db.Tag.find({$or: [{"word":"foo"}]})
and the second one does not give any results.
There's some kind of magic I obviously don't understand :( What is wrong in second query?
in theory, $or requires two or more parameters, so I can fake it with:
db.Tag.find({$or: [{"word":"foo"},{"word":"foo"}]})
but still, no results.
Your second query is perfectly fine, and it should work. Though the docs says, that $or performs logical operation on array of two or more expression, but it would work for single expression also.
Here's a sample that you can see, and try out, to get it to work: -
> db.col.insert({"foo": "Rohit"})
> db.col.insert({"foo": "Aman", "bar": "Rohit"})
>
> db.col.find({"foo": "Rohit"})
{ "_id" : ObjectId("50ed6bb1a401d9b4576417f7"), "foo" : "Rohit" }
> db.col.find({$or: [{"foo": "Rohit"}]})
{ "_id" : ObjectId("50ed6bb1a401d9b4576417f7"), "foo" : "Rohit" }
So, as you can see, both your query when used for my collection works fine. So, there is certainly something wrong somewhere else. Are you sure you have data in your collection?
Okaay, server admin installed mongodb from debian repo. Debian repo had 1.4.4 version of mongodb, aandd looks like $or is simply not yet supported out there :P

Why did this line delete everything in my MongoDB database?

Ok, so I'm trying to roll out a small update to my site. One update includes querying upon a field that may or may not exist. This doesn't work as I want, so I decided to just make it so that the field always exists in my database. I used this line at the MongoDB shell:
> db.entries.update({Published: null},{$set: {Published: true}},false,true);
Now, I'm not fully understanding how this caused every entry object where Published is null to be deleted. I mean, it literally was deleted. I tried looking up some IDs, and .findOne will return null for them.
How does that line work? I thought it would take every entry where Published is null(doesn't exist), and set Published to true.
Reading about operator behavior is better than guessing operator behavior. Search for null is different from performing a check for existence.
MongoDB has a dedicated $exists operator:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-%24exists
To be honest, I'm not sure why it now works with changes, or at least, why it deleted everything with that command.
My ending command looked like this:
db.entries.update({Published: {$exists: false},$atomic: true},{$set:{"Published":true}},false,true);
I thought it would take every entry where Published is null(doesn't exist), and set Published to true.
OK, so these are two different things.
Published is null:
{ Published : null, post : 'blah' }
Published does not exist:
{ post : 'blahblah' }
You may want to post this question over at the MongoDB user group (developers check it very often) at http://groups.google.com/group/mongodb-user
Updates do not delete documents. In fact, the update you ran does what you intended, for example, if you wanted y to always have a value:
> db.foo.insert({x:1})
> db.foo.insert({x:2})
> db.foo.insert({y:null})
> db.foo.insert({y:1})
> db.foo.update({y:null},{$set : {y:true}}, false, true)
> db.foo.find()
{ "_id" : ObjectId("4db02aabbe5a5418fb65d24c"), "y" : true }
{ "_id" : ObjectId("4db02aafbe5a5418fb65d24d"), "y" : 1 }
{ "_id" : ObjectId("4db02aa1be5a5418fb65d24a"), "x" : 1, "y" : true }
{ "_id" : ObjectId("4db02aa4be5a5418fb65d24b"), "x" : 2, "y" : true }
There must have been another operation that did the delete. There might be a record of it in the logs (or there might not... it depends how long it took). It's impossible to tell from the info here what caused the deletions, but the update isn't the culprit here.