How to alter _id field of MongoDB? [duplicate] - mongodb

This question already has answers here:
How to update the _id of one MongoDB Document?
(7 answers)
Closed 8 years ago.
I am working on a project using MongoDB and storing users data with their email as _id field.
Now realizing that that is a stupid idea, I wanna add an extra field, let's say "userid" as the new _id field of this collection and let the email as regular field.
Is it possible to do that?
Thank you

It is not possible tp update the _id of a document.
Currently (and probably for the forseeable future) the only way to modify the _id field is to actually remove your old document and reinsert with a new _id:
// Insert our test doc
db.c.insert({_id:1,name:'sammaye'});
// Get it back out
doc=db.c.find({name:'sammaye'});
// Now lets update by removing it first
db.c.remove({_id:doc._id});
// Change the old _id to a new one
doc._id=2;
// Reinsert it and now it has new _id
db.c.insert(doc);

Related

How to create new document if doesn't exist and delete if does existed by _id in MongoDB

I'm working with Mongoose/Mongodb and I'm facing a situation. Specifically, I want to create a new document if this document doesn't existing which condition by _id and else will delete if it does.
I needs to the help from everyone!

Should i create index on nested _id column in mongodb [duplicate]

This question already has answers here:
Does mongodb automatically create an index on the _id field of embedded documents?
(3 answers)
Closed 1 year ago.
I have nested array of objects with _id inside. I know, that collection has default, top level, index created on _id column. Mu question is - should i create index on nested _id columns or mongodb has a convention that all _id named columns will be indexed?
Thanks
if you want you should create an index for nested _id
mongo by default index _id at root level

How can I update a document list field without overriding it in MongoDB? [duplicate]

This question already has answers here:
MongoDB - Update or Insert object in array
(10 answers)
Closed 2 years ago.
I have the following document:
{
"_id":ObjectId("idhere"),
"locations":[
{
"latitude":3453.4353,
"longitude":31.123
},
{
"latitude":1243.134,
"longitude":992.321
}
]
}
The question is:
How can I update this document locations' without overriding the entire list,just by appending a new location to the existing ones? I know that to update a document I have to use this query:
db.collection.update({ _id : ....}, {$set : { locations : ... }}) but here is the problem -> in this way I should get the existing locations before, add the new location to this array and execute the query with the updated array which would take a lot of time & resources if the locations array grows.
Any suggestions? (P.S.: I'm currently using the MongoDB in a spring-boot project and I'm using MongoRepository interface to communicate with the DB.)
I've found the solution for this question in the mongodb docs here at the "Examples" section.
A more detailed picture here:
The saving solution is to use $push instead of $set.

Update specific field in all documents in MongoDB [duplicate]

This question already has answers here:
Update field in exact element array in MongoDB
(5 answers)
MongoDB: How to update multiple documents with a single command?
(13 answers)
Update MongoDB field using value of another field
(12 answers)
Closed 5 years ago.
I have a collection in MongoDB that contains a list of objects. Each object has an array of "updaters" which are objects with a field that I want to update. One of the updaters in that array needs a description updated. How can i update just the descrption of the the updater object, that I know "updaterId" of.
Update:
It appears to me that setting the flag multi: true will do the trick in Mongo 2.2+.
db.configurations.update(
{"updaters.updaterId": "90391154-67BB-452E-A1A7-A07A98B94F86"},
{$set: {"updaters.$.description": "This tool will prevent users from Unloading Linked Revit files for \"all users\" which causes such Linked File to be unloaded by default when opening project."}},
{multi: true}
)
You can update the description updaterId like this:
db.collection.update( {"updaters.updaterId" : updaterId } ,
{$set : {"updaters.$.description" : "My description"}} ,
false ,
true);

Mongo FIND Query to return Collections where Two Of Document's Fields Contain The Same Values (Were One is an Array) [duplicate]

This question already has answers here:
MongoDB Aggregation - match if value in array
(9 answers)
Closed 6 years ago.
Lets say I have a collection called Messages and a message can have multiple parents which can be found by checking if the message_id is in the parent_id array, where the parent_id is an array.
{
message_id : 22,
parent_id: [22,11],
}
How would I create a mongo query to find that?
Right now I have the following but it fails:
db.messages.find({this.message_id: { $in: this.parent_id}})
As you probably know mongo db does not support joins, I recommend you to do this at application level, using mongo shell you could run something like this.
//Get the message in which you are interested to get their parents
var child = db.messages.findOne({message_id: 22});
//Go with the array to search the parents.
db.messages.find({message_id : { $in : child.parent_id }});
I think you need to do in two steps, unless somebody knows a more elegant solution.