Showing posts with label mongodb. Show all posts
Showing posts with label mongodb. Show all posts

Command Prompt Tutorial

Commands (Cont. 2 )


  •  You can even declare variables which may contain whole set of data.



  •  Now after defining the variable “doc”, we have to save it explicitly, by applying the save command over the data collection.



  •  Now, if you want to fetch a particular document from the collection, then use its index number. For instance:-



  •  You can use a variable to assign value to some other document.





  •   Indexing in mongoDB
           db.collection_name.ensureIndex({key:1});
           
here key is the file on which we want to perform the indexing and 1 represents ascending order and -1 represents descending order.




Command Prompt Tutorial


Commands (Cont. 1 )

You can check the no. of documents in a collection by using
> db.data.count()



Lets insert some documents into the collection "data", we have created
Just to clarify, our database name is data and our collection name is also data. 

( I should have used a different name for collection...my apologies...)


Syntax is:

db.collection_name.insert({"field":"value"});


>db.data.insert();

This is empty, which we have created.

So, lets insert some name

>db.data.insert({“name”:”abc”});

We can also specify multiple values for a single attribute.

>db.data.insert({“name”:”abc”,”ph”:[123,456]});



To check your documents in a collection, use the following command

>db.collection_name.find();




The unique ID above is generated based on the machine and current time. 
So, the combination of all these components makes a perfect unique key.