O'Reilly logo

PHP and MongoDB Web Development by Rubayeet Islam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action - creating an index on a MongoDB collection

Let's see how we can build an index on a field of a MongoDB collection using the mongo interactive shell.

  1. Launch the mongo interactive shell and switch to a database named testdb:
    $ ./mongodb/bin/mongo
    MongoDB shell version: 1.8.1
    connecting to: test
    > use testdb
    switched to db testdb
    >
    
  2. Insert information about some of your favorite movies in a collection named movies:
    > db.movies.insert({name: "The Matrix", genre:"sci-fi", year: 1998}) > db.movies.insert({name: "Lord of the Rings", genre:"fantasy", year: 2002}) > db.movies.insert({name: "Saving Private Ryan", genre: "war", year: 1997}) > db.movies.insert({name: "Goodwill Hunting", genre: "drama", year: 1995}) > db.movies.insert({name: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required