I am trying to insert data in mongoDB document but I am getting duplicate error in it.
While defining the schema, I have given the value of "role" as default:"customer".
But on adding data with different number, i am getting this error.
Following is the schema defined:
Where is the error?
Check if the collection have unique index on the column.
db.customers.getIndexes()
here you will get unique index on userName. drop it
It will return all the indexes. If it have unique: true then drop it and then insert records.
OR just use
db.customers.dropIndex('username_1')
It's not the error because of mobilenumber. it's because of username. you are trying to insert multiple username with null value which is violating unique rule.
Related
I just need an idea about this. I have created a table with a unique column named person_name. In this table I use prisma v2 soft delete method. In here just update the deleted column as true.
But when I try to insert the same person_name after a soft delete, it returns an error calling Unique constraint failed.
Can anybody give any idea to skip this issue. My requirement is to make person_name as a unique column. But when I delete the person_name I should be able to insert the same person_name again.
If you have a unique column index on the person_name field then even if you mark the record as soft deleted, it still exists in the table due to which you are getting the unique constraint failed error.
You actually need to create a partial unique index where only the fields which are not soft deleted are included in the unique index and the deleted ones are not included.
You can do something like this:
CREATE UNIQUE INDEX soft_delete ON user (name) WHERE (is_deleted is NOT true);
I have a product document in mongodb. I am retrieving data with paging using skip and limit after filtering and sorting, like this using mongoose -
Product.find({gender:'male'})
.sort(price: 1)
.skip(page * 25).limit(25);
When page value is 0 it returns the result as expected, but when the page value changes to 1 it returns some results that were already retrieved when page value was 0. I am passing all these parameters through query in nodjs express. How can i retrieve unique value only with sorting and filtering while using pagination with mongoose ?
As mentioned here:
MongoDB does not store documents in a collection in a particular order. When sorting on a field which contains duplicate values, documents containing those values may be returned in any order.
If consistent sort order is desired, include at least one field in
your sort that contains unique values. The easiest way to guarantee
this is to include the _id field in your sort query.
So you can solve the problem by:
Product.find({gender:'male'})
.sort({price: 1, _id: 1})
.skip(page * 25).limit(25);
When i am writing a query to get a document with multiple population.
one population id is empty, hence the entire query is throwing an error.
is there a way to get the data ignoring the missing population id?
I am getting errors when trying to insert new records into my database (Postgres) using Node.js with Objection.js as the ORM/db handler. I have followed the tutorial, but I cannot find a mention of this in their current docs.
Here are my errors:
When debugging to console: error: null value in column "id" violates not-null constraint
When receiving the error via Postman, e.g. (for searchability with search engines): error: null value in column "id" violates not-null constraint
It looks like the 'id' column is being inserted to with a 'null' value from the logged query; it should not be inserting at all because it uses a default sequence value (auto-identity field, in other SQL solutions).
How do I insert new entities?
It turned out that I was defaulting the id to null on the TypeScript/Javascript model, and Objection.js expects the property to not be defined.
So do one of the following:
Don't create the property (property, and thus value, will be undefined)
Create the property, but don't set a value (value will be undefined)
Set the value to undefined
delete the property
The reason it was setting it to null was because other ORMs I was trying supported setting it to null. Typeorm, in particular, allows marking a column as Primary and a Generated column (meaning it doesn't try to insert the value; the database's table definition is responsible for creating the value). Also, other non-JS ORMs also support either null or an invalid id value, such as zero, to indicate that the object does not yet exist in the table, and therefore should be inserted (again, with ways to indicate that the column is both primary and generated).
I tried creating hash index on my table in memsql using
CREATE INDEX hashindex USING HASH ON table (column);
But i get the following error
ERROR 1710 (HY000): MemSQL does not support non-unique hash indexes.
Am i missing something ?
In order to make that statement work, you would need to add the UNIQUE keyword between CREATE and INDEX. Ex:
CREATE UNIQUE INDEX hashindex USING HASH ON table (column);
If you are intentionally trying to have a non-unique hash index, however, it is not supported (as indicated in the error). If you are trying to have a unique index, then great! Adding the keyword will work for you. Just note that adding a unique index cannot be performed as an online operation.