brew tap mongodb/brew
brew install mongodb-community@6.0
brew services start mongodb-community@6.0
brew services stop mongodb-community@6.0
sudo apt-get install -y mongodb-org
``` sudo apt-get install gnupg curl
curl -fsSL https://pgp.mongodb.com/server-7.0.asc |
sudo gpg -o /usr/share/keyrings/mongodb-server-7.0.gpg
–dearmor
sudo apt-get update
sudo apt-get install -y mongodb-org
### setup docker
##### script handle
```shell
FROM python:latest
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY script2.py /
CMD [ "python3", "script2.py" ]
docker inspect -f '' mongo_docker
docker run hello-world
docker build -t test
docker run test
open --background -a Docker
docker images
docker pull mongo:latest
docker run -d -p 2717:27017 -v ~/MongoDB:/data/db --name mongo_docker mongo:latest
docker ps
docker exec -it mongo_docker bash
mongosh
db.createUser({ user: 'user', pwd: 'user', roles: [{ role: 'readWrite', db: 'testdb' }] })
mongosh
show dbs
use testdb
db
db.dropDatabase()
show collections
db.createCollection('col1')
db.col1.drop()
db.col1.insert({
'name':'mahima',
'roll':33
})
db.col1.insertMany([{
'name':'mahima',
'roll':33
},
{
'name':'nadika',
'roll':36
}])
db.col1.find()
db.col1.find().pretty()
db.col1.find({'name':'mahima'})
It updates the first available collection that matches the filter.
db.col1.updateOne({name:'mahima'},{ $set :{roll: 34}})
It updates all available collections that matches the filter.
db.col1.updateMany({name:'mahima'},{ $set :{roll: 34,name: 'Manoj'}})
db.col1.find({'name':'mahima'})
delete_result = collection.delete_many({})
print('Deleted', delete_result.deleted_count, 'documents')
pip3 install pymongo
from pymongo import MongoClient
# Connection URL
url = 'mongodb://localhost'
# Create a MongoClient
client = MongoClient(url)
#port
port = client.port
from pymongo import MongoClient
# Connection URL
url = 'mongodb://localhost:27017'
# Create a MongoClient
client = MongoClient(url)
# Get the database
db = client['testdb']
# Get the collection
collection = db['col1']
print("Collection:", collection)
from pymongo import MongoClient
url = 'mongodb://localhost:27017'
client = MongoClient(url)
db = client['testdb2']
db.create_collection('col3')
doc = db['col3']
doc.delete_many({})
r1=doc.insert_many([{'name':'nadika','rollno':1},{'name':'nabin','rollno':2}])
r2=doc.insert_one({'name':'mahima','rollno':33})
r3=doc.update_many({'name':'nabin'},{'$set':{'rollno':40}})
r4=doc.update_one({'rollno':1},{'$set':{'rollno':34}})
print(r1.inserted_ids)
print(r2.inserted_id)
print(r3.modified_count)
print(r4.modified_count)
client.close()
r1=doc.find()
for row in r1:
print(row)
r1 = doc.update_many({}, {'$unset': {'isd': ""}})
r2 = doc.find()
for row in r2:
print(row)
r2 = doc.find({'name': {'$regex': '^na'}})
student_table = document["student"]
for field_name, field_value in student_table.items():
print(field_name, field_value)
if field_name.endswith("_cs"):
print(field_name, field_value)
print(document["_id"])
doc.update_many(
{"_id": document["_id"]},
{"$unset": {f"teacherTable.{field_name}": ""}}
)
from pymongo import MongoClient
url = "mongodb://localhost:27017"
client = MongoClient(url)
db = client["test"]
doc = db["cps"]
doc.delete_many({})
doc.insert_many([
{
"name": "mahima",
"studentTable":{},
"teacherTable": {
"mahima_cs": [{"ObjectId": "12345"}],
}
},
{
"name": "nadika",
"studentTable":{},
"teacherTable": {
"nadika_cs": [{"ObjectId": "12345"}],
}
},
{
"name": "nadika",
"studentTable":{},
"teacherTable": {}
}
}]
)
r1=doc.find()
for row in r1:
print(row)
for document in doc.find():
#print(document)
if "teacherTable" in document:
student_table = document["teacherTable"]
# print(student_table)
for field_name, field_value in student_table.items():
if field_name.endswith("_cs"):
# print(field_name, field_value)
print(document["_id"])
doc.update_many
(
{"_id": document["_id"]},
{"$unset": {f"teacherTable.{field_name}": ""}}
)
r1=doc.find()
for row in r1:
print(row)