A
A
Alexander Sharomet2017-04-07 15:02:24
PHP
Alexander Sharomet, 2017-04-07 15:02:24

How to get a field by id in mongoDb if id is not an object?

Tell me how you can get the field by _id if _id is not ObjectId
If you save _id in the database as an object (ObjectId("11b12aa654bce41356002559")), then everything works, but if I want to set it myself, that is, just as a string ("11b12aa654bce41356002559") then this scheme finds nothing.

$product = Products::findOne(['_id' => '11b12aa654bce41356002559']);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
emp1re, 2017-04-07
@sharomet

Mongo - ObjectId id = ObjectId.get();
Mongoose - mongoose.Types.ObjectId
But please don't do that, create a separate field for your id, as _id is a unique field and is indexed by default for quick lookups.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question