If I wish to access a document by id, and I happen to know the id is say: 557afc91c0b20703009f7ede, why do I need to use the ObjectId function to query the database, like so:
db.mydocs.find(ObjectId("557afc91c0b20703009f7ede"))
rather than the apparently more obvious:
db.mydocs.find("557afc91c0b20703009f7ede")
or:
db.mydocs.find({_id: "557afc91c0b20703009f7ede"})
?
From mongodb documentation:
ObjectId is a 12-byte BSON type
ObjectId is therefore not a string. What you are actually doing is building this 12-byte object from its string representation.
Because when one row is inserted in db then one id is generated in ObjectId format not in string with column name _id.
Related
When I query all the records, i get the results as shown below.
When i copy an ID from the result and add it in the filter, no result is found.
Here is the example filter from documentation
What am i doing wrong? mongo db got to be kidding me :/
You are not providing the data type with the query. Since your _id field is ObjectId type and you are comparing as string that is why it is not working.
Change your filter condition and convert your string id to ObjectId type.
{_id: ObjectId('yourId')}
MongoDB provides an automatic unique identifier for the _id field in the form of an ObjectId data type.
I'm building a project with MongoDB, but I'm a little bit confused on how the _id field works. My understanding is that, by default, MongoDB will generate an object of type ObjectId and assign it as the id.
However, in my server, I'm assigning an arbitrary string as the _id of an object. What I haven't quite understood yet: Is this string somehow converted to an ObjectId, or does the type of the _id field change to string?
MongoDB will generate an _id field with an ObjectId value if an _id field does not exist in the document. If you create the _id by your own, then you can use whatever datatype you like to use. MongoDB does not modify it.
The only constraint is, it must be a unique value.
I have JSON from foreign MongoDB that contains fields starting with underscore like "_item" with hash of ObjectID.
I want to save such JSON to my local MongoDB but Mongoose tries to cast those fields to ObjectIDs (by following underscored fields convention https://gist.github.com/bclinkinbeard/5171359) and throwing MongooseError: Cast to ObjectID failed for value error.
In schema I define that field as String _item: {type: String}.
I want to store those fields as simple strings instead of references. Can I do that without transforming field names?
I have a Meteor application whereby I initially use the _id field from each record in my collection when naming list items in my template.
When get the _id field, I convert it to a string to use in the template.
Now I want to update these records in Mongo and am passing the _id back to a Meteor.method, but these are still in string format and Mongo is expecting an ObjectID(). Is there a simple way to convert this string to the ObjectID()? If not, what alternatives do I have?
Ok, found it! On the /server, within your Meteor method function do this to convert it:
var mid = new Mongo.ObjectID(str_id_sent_to_server);
I have an existing mongodb collection, which doesn't have any information about when the document was created.
Is it possible to get this information some how? I've had a look through the docs but can't see it anywhere.
If you are using the default ObjectId value for your _id attribute, the creation time is encoded inside it.
As stated in the ObjectID documentation:
ObjectId is a 12-byte BSON type, constructed using:
a 4-byte value representing the seconds since the Unix epoch,
a 3-byte machine identifier
a 2-byte process id, and a 3-byte counter, starting with a random value.
You can call the getTimestamp() function on an ObjectId object to get an ISODate object containing the creation time of the object:
In the mongo shell:
ObjectId().getTimestamp()
ISODate("2014-05-14T14:29:12Z")
There is most likely the actual _id value of the document unless you have replaced this with something else.
In the simple "JavaScript" syntax of this ( and various methods are available to other languages) you simply access this as:
var id = new ObjectId();
id.getTimetstamp();
Various language implementations have a way of retrieving the "timestamp" from an ObjectId value so you can just use that.