How to ignore duplicate "_id" and insert unique "_id" in mongodb - mongodb

I have a JSON structure at first insertion as
collection.insert(query.query, function(err, docs) {
callback(err,docs);
dbCon.close();
});
JSON Structure:
"employees":[
{"_id":1, "lastName":"Doe"},
{"_id":2, "lastName":"Smith"},
{"_id":3,"lastName":"Jones"}
]
When I first insert this JSON into mongodb, it gets inserted without any errors.
When I try insert again on the same database, with below JSON
"employees":[
{"_id":2, "lastName":"Smith"},
{"_id":5, "lastName":"Peter"},
{"_id":6,"``lastName":"James"}
]
Now the mondo db is throwing duplicate key error.
Is there any way where I can omit the "_id":2 and insert "_id":5 and "_id":6 into mongo?
Please help me..

You can perform an upsert in your case :
db.people.update(
{ name: "Andy" },
{
name: "Andy",
rating: 1,
score: 1
},
{ upsert: true }
)
If the first query gets a blank collection, that it will insert the data in this case if a Recrod with name Andy is not there, it will insert one.

In the first time, you insert a document
{"_id":2, "lastName":"Smith"}.
In the second time , you insert a document {"_id":2, "lastName":"Smith"}.
Their ids are the same ,not unique.
Suggest create id automatically by mongdb.
You can add the following code:
collection.findOne({"_id":2}.exec(function(err,result){
if(!result){
collection.insert(XXX);
}
}));

Related

How to refer currently updating records in mongoDB query?

Below is my collection
[{documentId: 123, id: uniqueValue }]
Expected result
[{documentId: 123, id: id1,uniqueKey: uniqueValue }]
How do I refer "id" column for currently updating records, also id column can be anything for which my outer query is giving me the column name
db.supplier.updateMany( { documentId : 123}, { $set: { "uniqueKey": id} } );
so in above query "id" is coming like outerObject.mapping.idColumn which I want to substitute in above query.
The whole point of doing this, is to create index on column, and current collection does not have fixed column name on which I want to fire a query
Example
There are two collections collectionOne and collectionTwo
for each document in collectionOne there are multiple document in collectionTwo. The docId is used for lookup.
collectionOne
[{
docId :123,
col1 : lookupColumn
metaData: "some metaData",
extra : "extra columns"
}, ... ]
collectionTwo
[{
docId :123,
lookupColumn:"1",
a:"A",
b:"B" ....
},
{ docId :123,
lookupColumn:"2",
a:"A",
b:"B" ....
}
{ docId :123,
lookupColumn:"3",
a:"A",
b:"B" ....},.....]
lookupColumn in collectionTwo may have different name and mapping of that name is given in collectionOne by col1 field (which is always same), in this example col1 value is lookupColumn so I want to create a column newKey and copy value of lookupColumn into it.
So I came up with below Query
db.collectionOne.find({}).forEach(function(obj) {
if(obj.columns) {
existingColumn =obj.columns.col1;
db.collectionTwo.updateMany( { docId: obj.docId}, { $set: { "newKey": existingColumn} } );
}
}
problem is I am not able to pick an existing column name using variable existingColumn, I have tried using $ as well, which inserts $"existingColumn" as newKey value.
I have updated query with one more loop over collectionTwo but I feel that in optimized and unnecessary.
To go from
{documentId: 123, id: uniqueValue }
to
{documentId: 123, id: id1, uniqueKey: uniqueValue }
Use the pipeline style of update, which lets you use aggregation syntax:
db.collection.update({documentId: 123}, [{$set:{uniqueKey:"$id", id:"id1"}}])
EDIT
The latest edit to the question makes this a lot more clear.
You were almost there.
In MongoDB 4.2, the second argument to updateMany accepts either an update document like you were using:
db.collectionTwo.updateMany( { docId: obj.docId}, { $set: { "newKey": existingColumn} } );
Or it can accept an aggregation-like pipeline, but not all stages are available. For this use, if you make that second argument an array so that it is recognized as a pipeline, you can use the "$variable" structure. Since you already have the field name in a javascript variable, prepend "$" to the fieldname:
db.collectionTwo.updateMany( { docId: obj.docId}, [{ $set: { "newKey": "$" + existingColumn} }] );

Update and insert together in mongodb using pymongo?

I have a list of dictionaries as follows:
data=[{
"uni_id": '101',
"name":"abc",
"age": 10,
},
{
"uni_id": '102',
"name":"def",
"age": 12,
}]
I want to use this dictionary to update my database only if the "uni_id" exists in the database, if it doesn't exist in my database then I want to insert that particular "uni_id" and also its corresponding "name" and "age":
My update statement is as follows, however I would also like to use insert with it to satisfy the above mentioned conditions. How to write the insert statement? Or is there a way to write some if-else statements to do update and insert?
db.students.update_one(
{"uni_id":data[0]['uni_id']},
{
"$set":
{
"name":"abc_1",
}})
Set upsert to true... It creates a new document if the updated document is not existed in the collection
db.students.update_one(
{ "uni_id": data[0]['uni_id']},
{ "$set": { "name":"abc_1" }},
True
)

MongoDB Update from Aggregate

I am extremely new to Mongo and need some up creating an update statement. I have two different collections. I need to update the one collection's values with the results from my aggregate query where the id's match.
Here is my aggregate query that gives me the id for the other collection and the value I need to set it to:
db.ResultsCollection.aggregate(
{$group:{_id:"$SystemId", "maxValue": {$max:"$LastModified"}}}
);
How do I loop through the other collection with this data and update where the _id matches the SystemId from my aggreagate?
UPDATED CODE:
db.ResultsCollection.aggregate(
{$group:{_id:"$SystemId", "maxValue": {$max:"$LastModified"}}}
).forEach(function(
db.CollectionToUpdate.updateOne(
{ _id : doc._id },
{ $set: {UpdateDate: doc.maxValue } },
{ upsert: false }
);
});
My updated code does not generate a syntax error, but does not update the results when I refresh.

Create case sensitive index with mongoDB?

I'm trying to create case sensitive index with mongoDB version 3.4? I'm using the following query to create an index but still it allows me to insert data with different case?
db.Test.createIndex( { "type" : 1 },{ unique: true , collation: { locale: 'en' ,caseLevel:true ,strength: 3 } } )
in the above query i'm making Type as unique. First i inserted "apple" into the database and when i try to "apple" it throws duplicate error. but when i try to insert "Apple" it allows me to insert. for me while inserting "Apple" it should throws duplicate error.
Strength 2 will work
db.Test.createIndex({
type:1
},
{
collation:{
locale:"en",
strength:2
},
unique:true
}));

mongodb: exclude documents that contain a particular attribute during query

I have a mongo collection with data as follows:
(userid, orgid, some_attr)
g#gmail.com, 12345, 'xxxxxx'
Same collection also contains:
(orgid, some_attr)
12345, 'yyyyy'
Now when I am updating data I may query on (userid, orgid) or (orgid).
My issue is:
when I update using query (orgid), it finds the record with userid and updates that. I dont want that.
How can I query on (orgid) but exclude all documents which contains
(userid, orgid).
For eg:
when I query { orgid : 12345 } it should only return { 12345, 'yyyyy' } and not
{ 'g#gmail.com' , 12345, 'xxxxx' }
Please provide pointers.
http://docs.mongodb.org/manual/reference/operator/query/exists/
Change your query:
{ orgid : 12345, userid: {$exists: false} }