假设我有一个BlogPost包含零到多嵌入式Comment文档的模型.我可以查询并让MongoDB 只 返回Comment与我的查询规范匹配的对象吗?
例如,db.blog_posts.find({"comment.submitter": "some_name"})仅返回评论列表.
编辑:一个例子:
import pymongo
connection = pymongo.Connection()
db = connection['dvds']
db['dvds'].insert({'title': "The Hitchhikers Guide to the Galaxy",
'episodes': [{'title': "Episode 1", 'desc': "..."},
{'title': "Episode 2", 'desc': "..."},
{'title': "Episode 3", 'desc': "..."},
{'title': "Episode 4", 'desc': "..."},
{'title': "Episode 5", 'desc': "..."},
{'title': "Episode 6", 'desc': "..."}]})
episode = db['dvds'].find_one({'episodes.title': "Episode 1"},
fields=['episodes'])
Run Code Online (Sandbox Code Playgroud)
在这个例子中,episode是:
{u'_id': ObjectId('...'),
u'episodes': [{u'desc': u'...', u'title': u'Episode 1'},
{u'desc': u'...', u'title': …Run Code Online (Sandbox Code Playgroud) 我正在尝试从ObjectID获取时间戳,但Mongo一直给我这个错误.我错过了一个导入?将时间戳转换为标准日期格式的最佳方法是什么?
video['date'] = video['_id'].getTimeStamp()
Run Code Online (Sandbox Code Playgroud) 我对以下问题感到困惑.
我正在使用Flask,flask-pymongo扩展,mongodb版本v2.2.0-rc0,pdfile版本4.5
这是我的路线:
@app.route("/check/<id>")
def check(id):
doc=conn.db.msg.find_one({'_id':id})
return render_template('base.html',name=doc)
Run Code Online (Sandbox Code Playgroud)
id是msg集合中文档的有效_id,但总是返回None.
我试过了:
有什么想法吗?
更新:这个完整的URL如何:
http://127.0.0.1:8000/check/5030b628895b800de1a9a792
Run Code Online (Sandbox Code Playgroud)
UPDATE2:
我发现了类似的问题(回答)红宝石.不知道我怎么能把它翻译成python,我需要什么样的导入/模块?
更新3:我试过:
import bson
@app.route("/check/<id>")
def check(id):
id2="'"+id+"'"
doc=conn.db.msg.find_one({'_id':bson.objectid(id2) })
return render_template('base.html',name=doc)
Run Code Online (Sandbox Code Playgroud)
但是我得到TypeError:'module'对象不可调用(它也不能用于id)
当我达到1500时,我会建议一个沮丧的标签:-S
UPDATE4:
最后我把它拿起来跑了!
这是我的解决方案:
import bson
@app.route("/check/<id>")
def check(id):
doc=conn.db.msg.find_one({'_id':bson.ObjectId(oid=str(id))})
return render_template('base.html',name=doc)
Run Code Online (Sandbox Code Playgroud) 我收到此错误:
TypeError:'Cursor'类型的对象没有len()
当我尝试执行时:
reply = db['test'].find({"date":{"$gt":date_query}} ,{"date":1,"route_id":1,"loc":1,"_id":0})
length = len(reply)
Run Code Online (Sandbox Code Playgroud) 我正在使用一个干净的Ubuntu服务器实例,并希望在我的virtualenv中安装一些python包.
我从命令'pip install -r requirements.txt'收到以下输出
Downloading/unpacking pymongo==2.5.2 (from -r requirements.txt (line 7))
Downloading pymongo-2.5.2.tar.gz (303kB): 303kB downloaded
Running setup.py egg_info for package pymongo
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named setuptools.command
Complete output from command python setup.py egg_info:
Traceback (most recent call last):
File "<string>", line 3, in <module>
ImportError: No module named setuptools.command
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in /home/redacted/env/build/pymongo
Storing complete log in …Run Code Online (Sandbox Code Playgroud) 好一个字段匹配我运行:
db.bios.find( { "Country":"Netherlands" } )
Run Code Online (Sandbox Code Playgroud)
我如何携带所有文件而不是带文件"Country":"Netherlands"?
也可以带上所有文件,但没有2个国家?
什么是db.collection.find(标准,投影)的PyMongo方式.我找不到任何用于指定collection.find()方法的投影的文档
我正在执行批量写入
bulk = new_packets.initialize_ordered_bulk_op()
bulk.insert(packet)
output = bulk.execute()
并得到一个错误,我解释为意味着数据包不是一个字典.但是,我知道这是一个字典.可能是什么问题呢?
这是错误:
BulkWriteError Traceback (most recent call last)
<ipython-input-311-93f16dce5714> in <module>()
2
3 bulk.insert(packet)
----> 4 output = bulk.execute()
C:\Users\e306654\AppData\Local\Continuum\Anaconda\lib\site-packages\pymongo\bulk.pyc in execute(self, write_concern)
583 if write_concern and not isinstance(write_concern, dict):
584 raise TypeError('write_concern must be an instance of dict')
--> 585 return self.__bulk.execute(write_concern)
C:\Users\e306654\AppData\Local\Continuum\Anaconda\lib\site-packages\pymongo\bulk.pyc in execute(self, write_concern)
429 self.execute_no_results(generator)
430 elif client.max_wire_version > 1:
--> 431 return self.execute_command(generator, write_concern)
432 else:
433 return self.execute_legacy(generator, write_concern)
C:\Users\e306654\AppData\Local\Continuum\Anaconda\lib\site-packages\pymongo\bulk.pyc in execute_command(self, generator, write_concern)
296 full_result['writeErrors'].sort( …Run Code Online (Sandbox Code Playgroud) 我正在开发一个python项目,在需求文件中我有三种不同类型的PyMongo
Flask-PyMongo==0.3.1
pymongo==2.7
flask-mongoengine==0.7.1
Run Code Online (Sandbox Code Playgroud)
如何定义我正在使用的版本?
所以我创建了一个Cursor对象
cdb=self.mongo['bleh_bleh_bleh_setup_here']
data=cdb[collection].find(query_commands_here)
Run Code Online (Sandbox Code Playgroud)
不要担心上面的语法.假设我可以成功创建这样的游标对象
我知道我可以for循环遍历对象,但我想要的只是这个对象的第一项.有没有比循环更有效的方法?
编辑:
为了使事情更清楚,'bleh_bleh_bleh_setup_here'只是连接到所需mongoDB的路径,'query_commands_here'就是这样的查询{field1:{'$gt':num1}, field2:{'$ne':num2}}.这条线
data=cdb[collection].find(query_commands_here)
Run Code Online (Sandbox Code Playgroud)
将给我一个Cursor对象,我可以循环迭代for.所以像
for item in data:
print item
Run Code Online (Sandbox Code Playgroud)
将打印出对象中的每个条目.它工作得很好.但是,根据文档,此游标对象应该有调用的方法.hasNext(),如果有下一个条目,则应该返回True.到目前为止,我还没有找到一种方法让它因某些奇怪的原因而起作用.data.next()确实给了我一个入口.我想确保我可以有这个条件,以确保我不要求.next()一个不包含任何内容的游标对象,虽然我不预见会发生这种情况,但我会认为它会在某个时刻发生.