Questions tagged [objectid]

The ID of an object. This is used, among other reasons, to refer back to the object at a later time in the program

Used especially in web programming where space on the stack is limited, objectids are used to refer to objects at different parts of the program, and possibly even across different user sessions, if some marshalling is implemented (as databases, etc).

The ID of an object can also be used to determine if two objects are distinct instances or are indeed the same object, in which case, any modifications made on one would be reflected in the other as well

481 questions
121
votes
6 answers

How to get the size of single document in Mongodb?

I encountered a strange behavior of mongo and I would like to clarify it a bit... My request is simple as that: I would like to get a size of single document in collection. I found two possible solutions: Object.bsonsize - some javascript method…
user1949763
  • 3,583
  • 3
  • 16
  • 12
85
votes
7 answers

How to convert objectid to string

I want to get the string character from an ObjectId object. I use pymongo. eg: ObjectId("543b591d91b9e510a06a42e2"), I want to get "543b591d91b9e510a06a42e2". I see the doc, It says ObjectId.toString(), ObjectId.valueOf(). So I make this code: from…
Simon
  • 1,033
  • 1
  • 8
  • 16
64
votes
2 answers

Difference between storing an ObjectId and its string form, in MongoDB

I'm a little confused by Mongo DB's use of ObjectIds. Sure they're great for creating IDs client-side that almost definitely don't conflict with other client-side created ids. But mongo seems to store them in some special way. Storing a string…
B T
  • 57,525
  • 34
  • 189
  • 207
39
votes
4 answers

What is the difference between Model.findOne() & Model.findById() in Mongoose?

Consider we are searching a document from MongoDB based on the _id value. Which one of the following code is efficient ? ModelObj.findById(IdValue).exec(callback); ModelObj.findOne({ '_id': IdValue}).exec(callback); I feel ModelObj.findById() is…
Amol M Kulkarni
  • 21,143
  • 34
  • 120
  • 164
34
votes
3 answers

Is MongoDB _id (ObjectId) generated in an ascending order?

I know how the _id column contains a representation of timestamp when the document has been inserted into the collection. here is an online utility to convert it to timestamp: http://steveridout.github.io/mongo-object-time/ What I'm wondering is if…
Zahra
  • 6,798
  • 9
  • 51
  • 76
26
votes
8 answers

How to return the ObjectId or _id of an document in MongoDB? and error "$in needs an array"

I have a document in MongoDB and I would like to get the ObjectId of this document, but I did not find so far a method that does this to me. Example of query : user= db.users.find({userName:"Andressa"}) This returns this : { "_id" :…
Andressa Pinheiro
  • 1,517
  • 2
  • 18
  • 28
23
votes
4 answers

Primitive.ObjectID to string in Golang

I am trying to convert type primitive.ObjectID to string type in Go. I am using mongo-driver from go.mongodb.org/mongo-driver. I tried using type assertion like- mongoId := mongoDoc["_id"]; stringObjectID := mongoId.(string) Which VSCode accepts.…
Rabi
  • 359
  • 1
  • 2
  • 7
22
votes
6 answers

How to parse ObjectId in a pydantic model?

I am trying to parse MongoDB records to a pydantic model but failing to do so for ObjectId From what I understood, I need to setup validator for ObjectId and did try to both extend ObjectId class and add the validator decorator to my class using…
roshii
  • 507
  • 2
  • 4
  • 10
18
votes
2 answers

MongoDB storing arrays of ObjectId's

In my database I have to store an array of object ids. What should I use? Something like this: [ObjectId("50350e12a36feb1be6000364"), ObjectId("57350e12a37fef1be6000922"), ObjectId("10350e17d34ffb1be6200925")] or something like this:…
Francisco Costa
  • 6,713
  • 5
  • 34
  • 43
16
votes
1 answer

Why was the object_id for true and nil changed in ruby2.0?

I came across this ruby object_id allocation question sometime back and then read this awesome article which talks about VALUE and explains why object_id of true, nil and false the way they are. I have been toying with ruby2.0 object_id when I found…
Bharadwaj Srigiriraju
  • 2,196
  • 4
  • 25
  • 45
15
votes
9 answers

"ObjectId' object is not iterable" error, while fetching data from MongoDB Atlas

Okay, so pardon me if I don't make much sense. I face this 'ObjectId' object is not iterable whenever I run the collections.find() functions. Going through the answers here, I'm not sure where to start. I'm new to programming, please bear with…
nathanielsenje
  • 151
  • 1
  • 1
  • 4
14
votes
1 answer

MongoDB native: is there any difference between toString and toHexString methods?

I'm using Node.js v0.12.0 with MongoDB driver v1.4.34. So, is there any difference between converting ObjectID to String with toString and toHexString methods?
JustLogin
  • 1,822
  • 5
  • 28
  • 50
13
votes
8 answers

How to validate for ObjectID

Using Joi schema validation, is it possible to validate against MongoDB ObjectID's? Something like this could be great: _id: Joi.ObjectId().required().error(errorParser),
Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96
13
votes
1 answer

What is the difference between these two Ruby symbols?

I discovered this after playing around with object ids. ObjectSpace._id2ref(2648) => :** ObjectSpace._id2ref(6688) => :** ObjectSpace._id2ref(2648) == ObjectSpace._id2ref(6688) => false The first one is the symbol for the exponentiation…
Alex Altair
  • 3,246
  • 3
  • 21
  • 37
12
votes
2 answers

Mongotemplate - Query ObjectId according to greater than (gt) or less than (lt) operator

When I type this into my consol, it works: db.posts.find({"_id": {$lt:ObjectId("55732dccf58c555b6d3f1c5a")}}).limit(5).sort({"_id":-1}) When I use mongotemplate, it doesn't work and returns a blank array: @RequestMapping(value="/next",…
Simon
  • 19,658
  • 27
  • 149
  • 217
1
2 3
32 33