Azure CosmosDb with mongo error - "MongoError: query in command must target a single shard key" - mongodb

I have a cosmosdb database with Sharedkey. When my service run, it remove all documents by sharedkey field and insert the next. But during tests I had duplicateds inserts without errors notifys.I't have permission to directly delete the collection. This's customer envirronment and has process to change database.
I'd like to remove all collection documents. But has duplicated SharedKey fields and throw this error message:
MongoError: query in command must target a single shard key
When list by shared key
When try to remove by shared key
Hey friends somebody can "give me a hand"?
Obrigado!!!

Just like what some engineer said in the website I mentioned in the comment,
We have got the same feedback from PG team that you got on the support
ticket that you drop the entire collection using drop() instead of
deletemany(). So please follow the same for getting this issue
resolved.
So the solution to your error is use drop() instead.

To remove all collection documents, you can use the 'drop()' command. For more information on this, you can go through the MongoDB Documentation on drop()

It was a bug. To fix i had to remove all data and move to another collection with once sharedkey.
The best solutions was to change to Atlas MongoDb 😋

Related

Creating unique index for CosmosDB with MongoDB API fails

I'm using Azure CosmosDB with the MongoDB API. I'm trying to execute the following:
db.createCollection('test')
db.test.createIndex({key: 1}, {name: 'key_1', unique: true})
However, doing so fails with the following error:
The unique index cannot be modified. To change the unique index, remove the collection and re-create a new one.
When reading about it in the documentation and on Stack Overflow, it's mentioned that you can only create a unique index for empty collections.
However, when I try the following command, it seems my collection is empty, so this apparantly isn't the reason why it isn't working:
db.test.find()
I tried to recreate the collection several times, but to no avail.
As per the format of the query mentioned, it seems to be like wildcard indexing style and unfortunately if it is true, then wildcard indexing is having limitation in creation of unique index. In Azure Cosmos DB API for MongoDB cannot use wildcard index. Because creating a wildcard index is like executing multiple specific fields.
https://learn.microsoft.com/en-us/azure/cosmos-db/mongodb/mongodb-indexing
Refer the above-mentioned link for reference.
In my case, the problem was caused by a limitation of the continuous backup of CosmosDb.
As stated in the documentation:
Azure Cosmos DB API for SQL or MongoDB accounts that create unique index after the container is created aren't supported for continuous backup. Only containers that create unique index as a part of the initial container creation are supported. For MongoDB accounts, you create unique index using extension commands.
I had to create my unique index using the custom commands.

How to handle databases or collection being created accidentally in mongoDB? [duplicate]

Is there a way to switch off the ability of mongo to sporadically create dbs and collections as soon as it sees one in a query. I run queries on the mongo console all the time and mistype a db or collection name, causing mongo to just create one. There should be a switch to have mongo only explicitly create dbs and collections. I can't find one on the docs.
To be clear, MongoDB does not auto create collections or databases on queries. For collections, they are auto created when you actually save data to them. You can test this yourself, run a query on a previously unknown collection in a database like this:
use unknowndb
db.unknowncollection.find()
show collections
No collection named "unknowncollection" shows up until you insert or save into it.
Databases are a bit more complex. A simple "use unknowndb" will not auto create the database. However, if after you do that you run something like "show collections" it will create the empty database.
I agree, an option to control this behavior would be great. Happy to vote for it if you open a Jira ticket at mongoDB.
No, implicit creation of collections and DBs is a feature of the console and may not be disabled. You might take a look at the security/authorization/role features of 2.6 and see if anything might help (although there's not something that exactly matches your request as far as I know).
I'd suggest looking through the MongoDB issues/bug/requests database system here to and optionally add the feature request if it doesn't already exist.
For people who are using Mongoose, a new database will get created automatically if your Mongoose Schema contains any form of index. This is because Mongo needs to create a database before it can insert said index.

stop mongodb creating dbs and collections dynamically

Is there a way to switch off the ability of mongo to sporadically create dbs and collections as soon as it sees one in a query. I run queries on the mongo console all the time and mistype a db or collection name, causing mongo to just create one. There should be a switch to have mongo only explicitly create dbs and collections. I can't find one on the docs.
To be clear, MongoDB does not auto create collections or databases on queries. For collections, they are auto created when you actually save data to them. You can test this yourself, run a query on a previously unknown collection in a database like this:
use unknowndb
db.unknowncollection.find()
show collections
No collection named "unknowncollection" shows up until you insert or save into it.
Databases are a bit more complex. A simple "use unknowndb" will not auto create the database. However, if after you do that you run something like "show collections" it will create the empty database.
I agree, an option to control this behavior would be great. Happy to vote for it if you open a Jira ticket at mongoDB.
No, implicit creation of collections and DBs is a feature of the console and may not be disabled. You might take a look at the security/authorization/role features of 2.6 and see if anything might help (although there's not something that exactly matches your request as far as I know).
I'd suggest looking through the MongoDB issues/bug/requests database system here to and optionally add the feature request if it doesn't already exist.
For people who are using Mongoose, a new database will get created automatically if your Mongoose Schema contains any form of index. This is because Mongo needs to create a database before it can insert said index.

BestPractice to Confirm Delete data from mongodb using springData

Just want to confirm the mechanism via which it can be confirmed that data has been deleted successfully from MongoDB.
By using following code I am able to delete a record successfully from the mongodb
Query query = new Query();
query.addCriteria(Criteria.where("id").is(id));
MongoConfig.getMongoTemplate().remove(query, Person.class);
But what if a record with provided id do not exist in the database. Even in that case mongoDB doesn't gives any error.
Now if I try to check that whether there was any error on not by using following code:
MongoConfig.getMongoTemplate().getDb().getLastError()
I am not too sure that in a multi threaded environment, what is the possibility that the error which is returned by mongodb if present is related to delete operation only.
As per second approach, first loading object with the provided id and then deleting it doesn't makes much sense to you as there will be unnecessary 2 queries will be fired.
Any pointers/help on same is highly appreciated.
MongoDB Java API is thread-aware/safe and multi-threading should not cause any issues with checking getLastError on the connection you did the remove.
If you are using newer version of API, it is very likely by default the getLastError check is already being done. What is the version of your API?

MongoDB unique index errors: how to know which field generated the error?

Update: this question was asked in regards to MongoDB 1.8.x and the accepted solution pertains to 1.8. Note, however, that Mongo 2.x has made changes to the error messages so that you can tell which fields generate the errors on the update as well as the insert (see comments from Kyle and Remon below).
Is there a way to attempt a Mongo upsert and, if there is a unique index violation, know which field is causing the problem--all in one database operation?
For example, say I have a customers collection with _id and name properties. Also, say a unique index exists for name to ensure that a no two customer docs ever have the same name.
Currently, I do two database operations to perform an upsert:
Query customers to see if a document already exists with the name that I'm about to insert/update.
Do the upsert if no conflict exists.
I'd like to be able to do this in a single operation and, if there's an error, know which field is causing the problem.
This is sort of possible if the operation is to create a new document. Mongo returns the error E11000 duplicate key error index: {index name}. It's a bit of a hack, but I can use a regex to parse out the index name (which in my case, has the field name in it).
However, when updating a document the error message has no information about the index or field. It's simply "E11001 duplicate key on update".
Does anyone else have suggestions about smart ways to do upserts and know which fields are causing unique index problems, if necessary? Hoping to avoid server-side stored JavaScript functions...
For insert, parsing error message is your best bet. For update, unfortunately, there is no better way except trying to check against existing data in the collection. Feel free to raise an enhancement at https://jira.mongodb.org though.