Examples
Below are various MongoDB statements.
Three database commands you should know.
show dbs – List all databases.
use db_name – Switches to db_name.
show collections – List all tables in the current selected database.
Help:
At last, uses help() to guide you how to do things in MongoDB.
All available commands.
db.help() – Shows help on db.db.collection.help() – Shows help on collection (table).
db.collection.function.help() – Shows help on function.
Create:
Implicitly created on first insert() operation. The primary key_id is automatically added if _id field is not specified.
db.users.insert( {
user_id: "abc123",
age: 55,
status: "A"
} )
However, you can also explicitly create a collection:
db.createCollection("users")
db.createCollection("users")
Alter:
Collections do not describe or enforce the structure of its documents; i.e. there is no structural alteration at the collection level. However, at the document level,
update() operations can add fields to existing documents using the $set operator.
db.users.update(
{ },
{ $set: { join_date: new Date() } },
{ multi: true }
)
update() operations can remove fields from documents using the $unset operator.
db.users.update(
{ },
{ $unset: { join_date: "" } },
{ multi: true }
)
Create Index:
db.users.ensureIndex( { user_id: 1 } )
db.users.ensureIndex( { user_id: 1, age: -1 } )
db.users.ensureIndex({username:1},{unique:true});
db.users.getIndexes()
Drop Index:
db.users.dropIndex({username:1})
Drop Collection:
db.users.drop()
Select:
db.users.find ()
db.users.find (
{ }, { user_id: 1, status: 1 }
)
db.users.find (
{ }, { user_id: 1, status: 1, _id: 0 }
)
db.users.find ({ status: "A" })
Specified Columns:
db.users.find(
{ status: "A" }, { user_id: 1, status: 1, _id: 0 }
)
Not Equal:
db.users.find(
{ status: { $ne: "A" } }
)
AND:
db.users.find(
{ status: "A",
age: 50 }
)
OR:
db.users.find(
{ $or: [ { status: "A" } ,
{ age: 50 } ] }
)
Greater:
db.users.find(
{ age: { $gt: 25 } }
Drop Index:
db.users.dropIndex({username:1})
Drop Collection:
db.users.drop()
Select:
db.users.find ()
db.users.find (
{ }, { user_id: 1, status: 1 }
)
db.users.find (
{ }, { user_id: 1, status: 1, _id: 0 }
)
db.users.find ({ status: "A" })
Specified Columns:
db.users.find(
{ status: "A" }, { user_id: 1, status: 1, _id: 0 }
)
Not Equal:
db.users.find(
{ status: { $ne: "A" } }
)
AND:
db.users.find(
{ status: "A",
age: 50 }
)
OR:
db.users.find(
{ $or: [ { status: "A" } ,
{ age: 50 } ] }
)
Greater:
db.users.find(
{ age: { $gt: 25 } }
)
Less:
db.users.find(
{ age: { $lt: 25 } }
)
Greater and Less:
db.users.find(
{ age: { $gt: 25, $lte: 50 } }
)
Like:
db.users.find( { user_id: /bc/ } )
db.users.find( { user_id: /^bc/ } )
Sort ASC:
db.users.find( { status: "A" } ).sort( { user_id: 1 } )
Sort DESC:
db.users.find( { status: "A" } ).sort( { user_id: -1 } )
Count:
db.users.count()
or
db.users.find().count()
db.users.count( { user_id: { $exists: true } } )
or
db.users.find( { user_id: { $exists: true } } ).count()
db.users.count( { age: { $gt: 30 } } )
or
db.users.find( { age: { $gt: 30 } } ).count()
Distinct:
db.users.distinct( "status" )
Limited Results:
db.users.findOne()
or
db.users.find().limit(1)
db.users.find().limit(5).skip(10)
Explain:
db.users.find( { status: "A" } ).explain()
Update Records:
db.users.update(
{ age: { $gt: 25 } },
{ $set: { status: "C" } },
{ multi: true }
)
db.users.update(
{ status: "A" } ,
{ $inc: { age: 3 } },
{ multi: true }
)
Delete Rcords:
db.users.remove( { status: "D" } )
db.users.remove({})
No comments:
Post a Comment