-
Notifications
You must be signed in to change notification settings - Fork 1
/
MongoDB_Python
68 lines (33 loc) · 2 KB
/
MongoDB_Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
Starting MongoDb-->
import pymongo
Where faaiz == username and 1400 == password cluster0 == cluster name
client = pymongo.MongoClient("mongodb+srv://faaiz:[email protected]/?retryWrites=true&w=majority")
db = client["Testdb"] ---- Selects Database i.e. Testdb
collection = db["users"] --- Selects table i.e users.
Working:
collection.function_name()
collection.find() -- finds all users
collections.find_one({"name" : "Faaiz Ali"}) etc ---- To filter out user based on some criteria
function_name = insert_one / insert_many / delete_one / delete_many / update_many / update_one
Syntax ----> update_one(user , {"$set" : {"key" : "New Value"}}) or update_one(user, change) - change = new_query
syntax ----> insert_many -- takes list of dictionaries containing users or data etc
Query Syntax ---> query = {"income": {"$lt": 2000}} then pass this query to any function related...
Comparison operators:
$eq: Matches documents where the value of a field equals a specified value.
$ne: Matches documents where the value of a field does not equal a specified value.
$gt: Matches documents where the value of a field is greater than a specified value.
$gte: Matches documents where the value of a field is greater than or equal to a specified value.
$lt: Matches documents where the value of a field is less than a specified value.
$lte: Matches documents where the value of a field is less than or equal to a specified value.
Update Operators
Update operators are used to modify the values of fields in documents. Some commonly used update operators are:
$set: Sets the value of a field.
$unset: Removes a field from a document.
$inc: Increments the value of a field by a specified amount.
$push: Adds an element to an array.
$pull: Removes all elements that match a specified condition from an array.
Here Important thing is to use:
from bson.objectid import ObjectId
mongoDb has a default unique "_id"
id = input("Enter ID: ")
user_id = ObjectId(id) --- to create an id matchable with the database _id