Python mongoDB 的简单操作
#!/usr/bin/env python
# coding:utf-8
# Filename:mongodb.py from pymongo import MongoClient,ASCENDING,DESCENDING
import datetime # connection with mongoclient
client=MongoClient() # getting a database
db=client.test # getting a collection
collection=db.posts # documents
post={"author":"Mike",
"test":"My first blog post!",
"tags":["mongodb","python","pymongo"],
"date":datetime.datetime.utcnow()
} # inserting a document
post_id=collection.insert(post)
print 'posts id is:',post_id
print 'collection_names is:',db.collection_names() # getting a single document
doc=db.posts.find_one()
print doc #query by objectId
print 'query is:', db.posts.find_one({"_id":post_id}) # querying for more than one doc
for post in db.posts.find():
print post # counting
print 'total count is:',db.posts.count() # range queries
d=datetime.datetime(2014,8,9,12)
for post in db.posts.find({"date":{"$gt":d}}).sort("author"):
print 'gt is:',post # Indexing
# before indexing
print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["cursor"]
print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["nscanned"]
# after indexing
db.posts.create_index([("date",DESCENDING),("author",ASCENDING)])
print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["cursor"]
print db.posts.find({"date":{"$gt":d}}).sort("author").explain()["nscanned"] # remove all indexes
db.posts.drop_indexes()
Python mongoDB 的简单操作的更多相关文章
- NOSQL -- Mongodb的简单操作与使用(win10)
NOSQL -- Mongodb的简单操作与使用(wins) MongoDB 创建集合: db.createCollection(name, options) use huhu db.createCo ...
- NOSQL -- Mongodb的简单操作与使用(wins)
NOSQL -- Mongodb的简单操作与使用(wins) 启动mongodb: 1.首先启动服务 dos命令下:net start Mongndb 也可以查询服务,手动开启服务: 完成后: 2.启 ...
- mongodb的简单操作记录
由于项目上需要对mongodb进行监控,所以需要先熟悉下什么是mongobd以及mongodb的简单操作 mongodb的安装: curl -O https://fastdl.mongodb.org/ ...
- MongoDB数据库简单操作
之前学过的有mysql数据库,现在我们学习一种非关系型数据库 一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数 ...
- MongoDB的简单操作
一.简介 二.MongoDB基础知识 三.安装 四.基本数据类型 五.增删改查操作 六.可视化工具 七.pymongo 一.简介 MongoDB是一款强大.灵活.且易于扩展的通用型数据库 MongoD ...
- win7 安装 MongoDB 及简单操作
下载地址 http://dl.mongodb.org/dl/win32/x86_64 这里用的版本是 mongodb-latest-signed.msi 同时下载 mongodb-compass 下载 ...
- Python字符串的简单操作
数据的操作 字符串的一些常用操作: 1 1 #!/usr/bin/env python 2 # #coding=utf-8 3 # 4 # test='hello world' 5 # print(t ...
- MongoDB的简单操作(asp.net)
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MongoDB.D ...
- python文件备份与简单操作
#!/usr/bin/python # -*- coding: utf-8 -*- # data:2018/8/30 # user:fei import sys import random num = ...
随机推荐
- RT-thread内核之进程间通信
这里面见到的同步和互斥的概念非常清晰,转载自: http://www.cnblogs.com/King-Gentleman/p/4311582.html 一.进程间通信机制 rt-thread操作系统 ...
- Hadoop中FileSystem的append方法
今天在使用Hadoop 1.1.2版本进行FileSystem的append操作时报以下异常: org.apache.hadoop.ipc.RemoteException: java.io.IOExc ...
- Context Menu on DataGrid
应该设置 fitColumns: true 合并表头显示有问题 代码见示例
- Java邮件服务学习之四:邮箱服务客户端Spring Mail
一.Spring Mail API Spring邮件抽象层的主要包为org.springframework.mail,Spring提供的邮件发送不仅支持简单邮件的发送.添加附件. 1.邮件发送的核心接 ...
- ocp 1Z0-042 61-120题解析
61. View the Exhibit.Which statement regarding the dept and emp tables is true?A) When you delete a ...
- Define custom @Required-style annotation in Spring
The @Required annotation is used to make sure a particular property has been set. If you are migrate ...
- 几个代码片段-计算程序运行时间+获得当前目录+生成MD5
计算程序运行时间 long startTime = System.currentTimeMillis(); System.out.println("程序运行时间: " + (Sys ...
- 在JS和.NET中使用JSON (以及使用Linq to JSON定制JSON数据)
转载原地址: http://www.cnblogs.com/mcgrady/archive/2013/06/08/3127781.html 阅读目录 JSON的两种结构 认识JSON字符串 在JS中如 ...
- C#中ArrayList和string,string[]数组的转换
转载原地址: http://www.cnblogs.com/nextsoft/articles/2218689.html 1.ArrarList 转换为 string[] : ArrayList li ...
- ajax 传参 乱码问题
http://blog.csdn.net/yiyuhanmeng/article/details/7548505 开发一直用firfox网页,调试什么的都很方便.所以遇到了浏览器之间的兼容问题.url ...