Nipuna Chandimal
Nipuna Chandimal

Reputation: 405

How to find mongodb document by id without a search?

I need to find a document in mongodb using it's ID . This operation should be so fast. So we need to get the exact document which has the given ID . Is there any way to do that . I am a beginner here . So I would be much thankful to you if you could give some in-depth answer

Upvotes: 0

Views: 108

Answers (3)

Parikshit Chalke
Parikshit Chalke

Reputation: 4005

Okay so your are really a beginner.

First thing you should know from now that getting any kind of record from a database is done by a querying the database and its called as Search.

It simply means that when you want any data from your database, database engine has to search it in database using the query you provided.

So I think this sufficient to get you know that whenever you ask(using database query) database to give some records from it, it will perform a search based on conditions you provided, then it doesn't matter

  1. You provide condition with a single unique key or multiple complex combinations of columns or joins of multiple tables

  2. Your database contain no records or billions of records.

it has to search in database.

So above explanation holds true for I guess every database as far as I know.

Now coming to MongoDB

So referring to above explanation, MongoDB engine query database to get result.

Now main question is - How to get result Fast!!!

And I think that's what your main concern should be.

So query speed (Search Speed) is mainly depend on 2 things:

  1. Query.

  2. Number of records in your database.

1. Query

Here factors affecting are :

a. Nature of parameters used in a query - (Indexed or UnIndexed)

If you use Indexed parameters in your query it always going to be a faster search operation for database. For example the _id field is default indexed by mongodb. So if you search document in collection using _id filed alone is going to be always a faster search.

b. Combination of parameters with operators

This refers to number of parameters used for query (more the parameter, more slower) and kind of query operators you used in query (simple query operator give result faster as compare to aggregation query operators with pipelines)

c. Read Preferences

Read preference describes how MongoDB will route read operations to the members of a replica set. It actually describe your preference of confidence in data that you are getting.

Above are two main parameters, but there are many things such as :

  • Schema of your collection,

  • Your understanding of the schema (specifically data-types of documents)

  • Understanding of query operator you used. For example - when to use $or, $and operators and when to use $in and $nin operators.

2. Number of records in your database.

Now this happens when you have enormous data in database, of course with single database server it will be slower i.e. more records more slower.

In such cases Sharding (Clustering) your data on multiple database server will give you faster search performance.

MongoDB has Mongos component which will route our query to perfect database server in cluster. In order to perform such routing it uses config servers which stores the meta-data about our collections using Indexes and Shard-Key.

Hence in sharded environment choosing proper shard-key plays important role in faster query response.

I hope this will give you a descent idea of how actually a search is affected by various parameters.

I will improve this answer in future time.

Upvotes: 1

Oner T.
Oner T.

Reputation: 413

with mongoose:

router.get("/:id", (req, res) => {
  if (!mongoose.Types.ObjectId.isValid(req.params.id)) { // checking if the id is valid
    return res.send("Please provide valid id");
  }
  var id = mongoose.Types.ObjectId(req.params.id);
  Item.findById({ _id: id })
    .then(item=> {
      res.json(item);
    })
    .catch(err => res.status(404).json({ success: false }));
});

Upvotes: 0

Subburaj
Subburaj

Reputation: 5192

Its pretty starlight forward, you can try for the following:

var id = "89e6dd2eb4494ed008d595bd";
Model.findById(id, function (err, user) { ... } );

Upvotes: 0

Related Questions