How can i set an "id"-field in MongoDB-database with autoIncrement. I need unique numeric id.
I know, that there's simple method instead of counting items...
my application made with nodejs and expressjs framework
I have used mongoose auto increment plugin of mongoose for doing the same. It is quite helpful. You should have a look and give it a try.
Related
during the development of my app, i very often add custom and new fields to an existing schema, making the 'old' content in my mongodb 'incompelete' and missing the new fields. this leads sometimes to null content where it's required and it's in my use case very bad.
my question is what command/utility do i need to use to make mongoose validate my old documents, and in potential add those missing fields with pre-defined defaults to the old documents?
i remember reading something about that kind of functionality when i started learning how to use mongoose, but i just can't find it anywhere anymore..
thanks in advance :)
Amit
I am trying to create a slug(prettyurl) for each post added by the user. And use this slug to access the record in the db. The generated slugs might not be unique so I thought of adding the #rid at the end of the slug. So that the slugs will be unique and I can retrieve the record with the #rid while fetching the record. I can use this slug in the restful url's as well(after removing the # in the #rid).
So is there a way to append the rid to the slug property while inserting the record?
Or is there an auto increment field in orientdb which I can concatenate with the slug?
Or is there any other way to achieve the same result? I thought about generating a unique id from node js but this might add the overhead of creating and managing unique filed across multiple servers.
I am using
orientjs version: 2.1.0
orientdb version 2.1.6
Slug thing what you have explained I can not understand.
However #rid that is the Record Identifier in OrientDB is unique and it resembles like Primary Key of our Relational Database System.
to append the #rid you can use slug.append(#rid) but at the time of INSERT, it may not work as #rid is determined after the INSERT.
You can use INSERT.. RETURN #rid read it from here
However, for the Auto Increment purpose, I would say that #rid is automatically incremented. It is decided as #clusterIDOfTheRecord:positionOfTheRecordInTheCluster.
So, autoincrement there may not be needed.
I have Mongoose schema and I need to add a non-persistent field to it. The point of this field is to store some status, related to persistent data fields, but without need to store it to the database. I see that some Mongoose alternatives provide such a feature e.g. https://github.com/simpleviewinc/mongolayer#modeladdfieldargs, however I am not able to find similar one inside Mongoose.
Any tip would be greatly appreciated!
In mongoose you have the concept of virtual fields. See doc (http://mongoosejs.com/docs/guide.html#virtuals).
See also related topic at Adding 'virtual' variables to a mongoose schema?
I'm creating an API that is using data from Mongoose. The front end (Backbone) expects id, instead of _id. I cannot seem to find an easy solution to such a simple problem. Is anyone aware of a way to rename _id to id. I want this to be default behavior across every schema.
Did you think of setting model.idAttribute to _id on the front end (Backbone). This would allow Backbone to 'transparently map that key to id'.
http://backbonejs.org/#Model-idAttribute
You can set up a schema method getItem which returns the desired fields and id = _id, if you really need that :)
In past with my PHP / Rails - MYSQL apps I've used the unique ID of a table record to keep track of a record in an html file.
So I'd keep track of how to delete a record shown like this (15 being the ID of the record):
Delete this record
So now I'm using MongoDB. I've tried the same method but the objectID ._id attribute seems to be a loooong byte string that I can't use conveniently.
What's the most sensible way of binding a link in the view to a record (for deletion, or other purposes or whatever)?
If the answer is to create a new id that's unique for each document in the collection, then what's the best way to generate those unique id's?
Thank you.
You could use a counter instead of the ObjectID
But this could create a problem when inserting a new document after you deleted a previous one.
See this blog post for more detail info on Sequential unique identifiers with Node.js and MongoDB.
Or you could use the timestamp part of the ObjectID:
objectId.getTimestamp().toString()
See the node objectid docs