Insert_one no such method exists # pymongo 3.7.2 - python

I´m pretty new learning Python and all the containing stuff.
I tried to make my first little steps, installing a MongoDB (working) and connecting to it.
from pymongo import MongoClient
from pprint import pprint
from random import randint
client = MongoClient('localhost', 27017)
db = client.test
collection = db.users
user = {"id": 1, "username": "Test"}
user_id = collection.insert_one(user).inserted_id
print(user_id)
This is the complete code.
pymongo Version: 3.7.2 checked with:
pip freeze | grep pymongo
Output: pymongo==3.7.2
Python Version: 3.7.1
If I try to execute my tiny script, the following error occurs:
'Collection' object is not callable.
If you meant to call the 'insert_one' method on a 'Collection'
object it is
failing because no such method exists.
Where is my Fault?
A little research showed that in pymongo v2 the ".insert_one" is ".insert", but the 3.7.2 version is installed so I should (and must) use the ".insert.one", not the ".insert"

insert_one according with pymongo documentation exists, for server version >= 3.2...
the use is:
user = {'x': 1}
result = db.test.insert_one(user)
result.inserted_id
more complete explanation about insert_one:
>>> db.test.count_documents({'x': 1})
0
>>> result = db.test.insert_one({'x': 1})
>>> result.inserted_id
ObjectId('54f112defba522406c9cc208')
>>> db.test.find_one({'x': 1})
{u'x': 1, u'_id': ObjectId('54f112defba522406c9cc208')}
the following below, I executed and works fine:
# importing client mongo to make the connection
from pymongo import MongoClient
print("--- Exemplo pymongo Connection ---")
# Connection to MongoDB
client = MongoClient('localhost', 27017)
# Selection the Database
db = client.python
# Select the collection
collection = db.users
# Set up a document
user = {"id": 1, "username": "Test"}
# insert one document into selected document
result = collection.insert_one(user)
# Selection just one document from collection
#result = collection.find_one()
# removing the document inserted
collection.delete_one(user)
# print the inserted_id
print("inserted_id: ", result.inserted_id)
Pymongo Documentation

Related

How can I get the version information of mongodb through pymongo or other python tools?

As the title. It seems that pymongo does not support, is there any other easy way to get.
Once you are connected to your MongoDB via pymongo you can retrieve the version with:
db.command({'buildInfo':1})['version']
This would return e.g. '4.4.5'.
from pymongo import MongoClient
client = MongoClient(uri)
version = client.server_info()["version"]
version_array = client.server_info()["versionArray"]
print(version)
>>> '4.4.4'
print(version_array)
>>> [4, 4, 4, 0]

I wrote a pymongo script to add sample user but it is not working

import pymongo
client=pymongo.MongoClient("mongodb+srv://Debaditya:<password>#pycluster.d6kvk.gcp.mongodb.net/<db>?retryWrites=true&w=majority")
db = client.test
db.command("createUser", "sample", roles=["read"])
Help me pls the code is given above
Here is the sample code for inserting a document in a collection.
from pymongo import MongoClient
client = MongoClient("mongodb+srv://Debaditya:<password>#pycluster.d6kvk.gcp.mongodb.net/<db>?retryWrites=true&w=majority")
db = client.test
db.students.insert_one({"name": "Test User", "rollNo": 37})
In above example students is the collection name where you have inserted the above row.
If you want to list the rows in the collection.
list(db.students.find())
Just got through the documention of pymongo here

Connecting to MongoDB using PyMongo on Jupyter notebook

I have created an account with Atlas and now I am trying to retrieve some information from the database. My native language is R, so I inserted the "iris" data set into the collection called "table".
To show that there is data in my new database "test", I use the mongolite command (this is the R pymongo):
#m is my client connection
m$count()
[1]5490
The problem is connecting with python. I am currently on Jupyter Notebook. Here is the code.
import pymongo as pm
import pprint
import requests
url= "mongodb://jordan:*************#jordandb-shard-00-00-ykcna.mongodb.net:27017,jordandb-shard-00-01-ykcna.mongodb.net:27017,jordandb-shard-00-02-ykcna.mongodb.net:27017/test?ssl=true&replicaSet=JordanDB-shard-0&authSource=admin&retryWrites=true"
client = pm.MongoClient(url)
print(client)
[out]Database(MongoClient(host=['jordandb-shard-00-02-ykcna.mongodb.net:27017', 'jordandb-shard-00-01-ykcna.mongodb.net:27017', 'jordandb-shard-00-00-ykcna.mongodb.net:27017'], document_class=dict, tz_aware=False, connect=True, ssl=True, replicaset='JordanDB-shard-0', authsource='admin', retrywrites=True), 'test')
#I am assuming this means I am connected
When I call any methods on the database I get the error.
db = client.test #test is name of collection
db.iris.find_one({})
ServerSelectionTimeoutError Traceback (most recent call last)
<ipython-input-15-ab74ef5f0195> in <module>()
----> 1 db.iris.find_one({})
/opt/conda/lib/python3.6/site-packages/pymongo/collection.py in find_one(self, filter, *args, **kwargs)
1260
1261 cursor = self.find(filter, *args, **kwargs)
-> 1262 for result in cursor.limit(-1):
1263 return result
1264 return None
I would like to be able to connect and start exploring the data in my "test" data set by using methods like, list_database_names(), list_collection_names() etc.. Thank you kindly
Try this: I am using a python kernel in a jupyter notebook though but the logic remains the same
from pymongo import MongoClient
# replace "USER" and "PASSWORD" with your user and password respectively.
client = MongoClient("mongodb+srv://USER:PASSWORD#cluster0-hhhbr.mongodb.net/test?retryWrites=true&w=majority")
# replace "local" with any database in your cluster
db = client.local
# replace "emps" with your collection's name
collection = db.emps
# you can use the collection e.g, but this will return a cursor
collection.find()
# To get the actual data do this or create a context
# Don't forget to delete the 3 previous lines of codes if you are going to
# create a context
with client as cl:
db = cl.local
collection = db.emps
for i in collection.find():
print(i)
To connect to mongodb using python from any editor
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["pyramids"] #pyramids is the database
mycol = mydb["invoices"] #invoice is the collection

Make a list with mongodb and python

I'm working with mongodb and python. So I have a huge JSON that is within the database in mongodb, and I wanted to make a list where he gave a "print" on the screen of the things I looked for.
db.getCollection('request').aggregate(
{$match : {"kind" :"qpxExpress#tripsSearch"}},
{$unwind:"$trips.tripOption"},
{$project: {"trips.tripOption.saleTotal":1}}
)
I use it to look inside the JSON, but I wanted one of the print on the screen with a code in python.
Here is a JSON exempo that I get the information:
http://codebeautify.org/jsonviewer/cb959272
I've tried to do something but doesn't work:
from pymongo import MongoClient
client = MongoClient()
cursor = db.getCollection('request').aggregate(
{$match : {"kind" :"qpxExpress#tripsSearch"}},
{$unwind:"$trips.tripOption"},
{$project:{"trips.tripOption.saleTotal":1}}
)
for saleTotal in cursor:
print(saleTotal)
Something like this should work (assuming your DB name is test):
from pymongo import MongoClient
client = MongoClient()
db = client.test
cursor = db.request.aggregate(
[
{$match : {"kind" :"qpxExpress#tripsSearch"}},
{$unwind:"$trips.tripOption"},
{$project:{"trips.tripOption.saleTotal":1}}
]
)
for saleTotal in cursor:
print(saleTotal)

Retrieve data from pymongo

I ve got a mongo database and I want to retrieve all documents with a cursor operation like in mongodb java api. I want to retrieve all username of database based on this cursor iteration. My code is like the above:
import pymongo
from pymongo import MongoClient
client = MongoClient('...', 27017)
db = client.test_database
db = client['...']
collection = db.test_collection
collection = db["..."]
result = collection.find()
obj = next(result, None)
if obj:
username= obj['username']
print username
I want for the collection to print all the usernames.
Just loop over the results and print the username. There's no reason to play with next()
.
for obj in collection.find():
print obj['username']

Categories

Resources