Lazy Loading in MongoDb
What is lazy loading in MongoDb
?
If we want to create a database, it is done like:
m.getDB(<dbName>).getCollectionNames()
due to lazy loading only. Can anyone explain开发者_开发知识库?
Lazy loading is not something that is a capability of the database itself. All that it means is that when the client issues a query via a driver(or the ORM), the ORM can choose to not load the entire object graph when the query is made. It may choose to make the query to the db only when the resultant object is actually used/accessed.
This is again, independent of the databases that can be used, and is something that is typically built into the ORM itself. this is done for optimization/performance reasons, if a portion of the object graph is not always accessed, then it is fine if we lazy load it in the couple of instances when it Is actually accessed. Now this means that there will be a query that the ORM fires off to load that data on access, and then return the call to the client code.
It is typically unto the app developer to specify which properties/parts of the object graph should be loaded eagerly and which should be loaded lazily.
Now keep in mind that some nosql options may have ORM capabilities that enable us to do this, but th most common scenario where you will see this is in the RDBMS world, and where full blown ORMs are very common.
You can lookup hibernate's lazy load/lazy fetching capabilities to get abetter idea of the concept in general.
Adding on to what Shekhar said, you need to do lazy loading in something like mongoose, which interfaces with MongoDB. I found a post here: Lazy Loading/More Data Scroll in Mongoose/Nodejs
Basically, using the Mongoose module, you can do something like this:
var lazyload = User.find().skip(10).limit(10);
This will take the User schema and load the second set of 10 elements (the limit function will limit the amount of objects loaded to 10 and the skip function is self-explanatory). In this way, you can increment how many objects you skip by to create a lazy-load system.
精彩评论