I try to get the _id field from the existing collection in MongoDB to my return method so I can use that oid to edit user document, but the include keyword prompts me to create a new method. Am using 3.12 and follow the projections for guidance. Any advice will be highly appreciated!
Hi @Pat_Yue and welcome in the MongoDB Community !
I’m not sure I completely understood your question but here is a little piece of Java that explains how to retrieve an ObjectId as a String and then reusing it to update the documents in the collection.
import com.mongodb.client.MongoClient;
import com.mongodb.client.MongoClients;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.Projections;
import com.mongodb.client.model.Updates;
import org.bson.Document;
import org.bson.types.ObjectId;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static com.mongodb.client.model.Filters.eq;
import static com.mongodb.client.model.Projections.include;
public class Community {
public static void main(String[] args) {
try (MongoClient mongoClient = MongoClients.create("mongodb://localhost")) {
MongoCollection<Document> coll = mongoClient.getDatabase("test").getCollection("coll");
coll.drop();
coll.insertMany(
Arrays.asList(new Document("name", "Max"), new Document("name", "Alex"), new Document("name", "Claire")));
List<Document> docs = coll.find().projection(include("_id")).into(new ArrayList<>());
System.out.println("Printing the ObjectIds from the 3 docs:");
docs.forEach(doc -> System.out.println(doc.get("_id")));
System.out.println("\nUpdating the 3 documents using their respective IDs:");
docs.forEach(doc -> {
String stringId = doc.get("_id").toString();
ObjectId objectId = new ObjectId(stringId);
coll.updateOne(eq("_id", objectId), Updates.set("hobby", "gaming"));
});
docs = coll.find().into(new ArrayList<>());
docs.forEach(doc -> System.out.println(doc.toJson()));
}
}
}
I think the code is pretty self explanatory but please feel free to ask me questions is something isn’t clear.
This is the output I get:
Printing the ObjectIds from the 3 docs:
5f8757f9e42cd148f41b29c8
5f8757f9e42cd148f41b29c9
5f8757f9e42cd148f41b29ca
Updating the 3 documents using their respective IDs:
{"_id": {"$oid": "5f8757f9e42cd148f41b29c8"}, "name": "Max", "hobby": "gaming"}
{"_id": {"$oid": "5f8757f9e42cd148f41b29c9"}, "name": "Alex", "hobby": "gaming"}
{"_id": {"$oid": "5f8757f9e42cd148f41b29ca"}, "name": "Claire", "hobby": "gaming"}
First it’s returning ALL the documents in the “User” collection to your Java (so potentially millions of documents) and then if ONE of the documents has the “_id” you are looking for, then you return this document.
The find() function takes a filter in parameter which you can leverage here to simplify your code and only fetch that ONE document you are looking for.
Also note that this function can benefit from the default _id index that exists in all the MongoDB collections by default, so it’s not doing a collection scan to find this one document in your collection.
If you did a search on the name, you would have to create an index on that field to avoid a collection scan.