Mongodb: Goo The Following Table Shows The Relationship of Rdbms Terminology With Mongodb
Mongodb: Goo The Following Table Shows The Relationship of Rdbms Terminology With Mongodb
The following table shows the relationship of RDBMS terminology with MongoDB.
RDBMS MongoDB
Database Database
Table Collection
Tuple/Row Document
column Field
Mysqld/Oracle mongod
mysql/sqlplus mongo
MongoDB
Show All Databases
show dbs
Drop
db.dropDatabase()
Create Collection
db.createCollection('posts')
Show Collections
show collections
Insert Row
db.posts.insert({
title: 'Post One',
body: 'Body of post one',
category: 'News',
tags: ['news', 'events'],
user: {
name: 'John Doe',
status: 'author'
},
date: Date()
})
Find Rows
db.posts.find({ category: 'News' })
Sort Rows
# asc
db.posts.find().sort({ title: 1 }).pretty()
# desc
db.posts.find().sort({ title: -1 }).pretty()
Count Rows
db.posts.find().count()
db.posts.find({ category: 'news' }).count()
Limit Rows
db.posts.find().limit(2).pretty()
Chaining
db.posts.find().limit(2).sort({ title: 1 }).pretty()
Foreach
db.posts.find().forEach(function(doc) {
print("Blog Post: " + doc.title)
})
https://docs.mongodb.com/manual/tutorial/project-fields-from-query-results/
https://stackoverflow.com/questions/25589113/how-to-select-a-single-field-for-all-
documents-in-a-mongodb-collection
Update Row
db.posts.update({ title: 'Post Two' },
{
title: 'Post Two',
body: 'New body for post 2',
date: Date()
},
{
upsert: true
})
Update Specific Field
db.posts.update({ title: 'Post Two' },
{
$set: {
body: 'Body for post 2',
category: 'Technology'
}
})
Rename Field
db.posts.update({ title: 'Post Two' },
{
$rename: {
likes: 'views'
}
})
Delete Row
db.posts.remove({ title: 'Post Four' })
Sub-Documents
db.posts.update({ title: 'Post One' },
{
$set: {
comments: [
{
body: 'Comment One',
user: 'Mary Williams',
date: Date()
},
{
body: 'Comment Two',
user: 'Harry White',
date: Date()
}
]
}
})
Add Index
db.posts.createIndex({ title: 'text' })
// If you had a collection with thousands of documents with no indexes, and then you query to
find certain documents, then in such case MongoDB would need to scan the entire collection to
find the documents. But if you had indexes, MongoDB would use these indexes to limit the
number of documents that had to be searched in the collection.
Text Search
db.posts.find({
$text: {
$search: "\"Post O\""
}
})
“IndexNotFound”
// https://www.guru99.com/working-mongodb-indexes.html