+91 81900 98289

MongoDB Create DB

This MongoDB Create DB lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

MongoDB creates a database when you first write to it. Naming it is not enough.

The example creates the name and a first collection so the database would exist on a real server.

Example 1: Start here

use training in the shell selects the database. The first insert creates it.

server = {} # a dictionary; each key has one value server["training"] = {"learner": []} # change this one field print(sorted(server)) # a new list, sorted

Example 2: Filter the rows

MongoDB Create DB is easier to see as a list of records. The filter keeps every row whose seats value is at least 10.

rows = [{"topic": "MongoDB Create DB", "seats": 22}, {"topic": "SQL", "seats": 12}] # a list; the values stay in this order print([row["topic"] for row in rows if row["seats"] >= 10]) # keep the items that pass the test

Example 3: Update one field

An update changes one field and leaves the rest of the record alone. On a database you would run an UPDATE or an update_one for the same change.

row = {"topic": "MongoDB Create DB", "seats": 22} # a dictionary; each key has one value row["seats"] = row["seats"] + 1 # change this one field print(row)

Example 4: Count the matches

Counting matches is the idea behind COUNT(*). Here the number 22 appears twice.

rows = [22, 4, 22] # a list; the values stay in this order print(sum(1 for value in rows if value == 22)) # keep the items that pass the test
use training in the shell selects the database. The first insert creates it.
Call Now 81900 98289