Thursday, October 17, 2019

Cómo insertar un ObjectId customizado en MongoDB

Como sabrás, cuando haces un insert, mongo genera el _id automáticamente, este _id viene a ser la "primary key" de tu documento. Sin embargo, si querés agregar un ObjectId personalizado, tenés que tener cuidado de seguir el length o tamaño requerido de un ObjectId que es de 24. Aquí te dejo un ejemplo.

Happy coding!
db.businessunits.insert(
[
{_id: ObjectId("000000000000000000000001"), name: "lizzy", phoneNumber: "61368424", email: "lizzy@test.com", state: "active", createdDate: new Date()},
{_id: ObjectId("000000000000000000000002"), name: "melissa", phoneNumber: "63489125", email: "melissa@test.com", state: "active", createdDate: new Date()},
{_id: ObjectId("000000000000000000000003"), name: "ericka", phoneNumber: "78996235", email: "ericka@test.com", state: "active", createdDate: new Date()},
{_id: ObjectId("000000000000000000000004"), name: "sandra", phoneNumber: "78961254", email: "sandra@test.com", state: "active", createdDate: new Date()}
]
)
view raw insert.json hosted with ❤ by GitHub