今天写python脚本的时候发现这样一个问题:

import os , string , datetime ,pymongo;
conn = pymongo.Connection("127.0.0.1",27017);
db = conn.nn;
coll = db.nn_res;
value = dict(name="user1",num="");
coll.save(value);
coll.insert(value);

  执行脚本之后会发现,save和insert只执行了一条,而且跟insert和save出现的先后顺序没有关系,然后我去查看官网上关于save和insert的说明,发现save的部分解释是这样的:

db.collection.save(document)

Updates an existing document or inserts a new document, depending on its document parameter.

The save() method takes the following parameter:
Parameter Type Description
document document A document to save to the collection. If the document does not contain an _id field, then the save() method performs an insert. During the operation, mongod will add to the document the _id field and assign it a unique ObjectId. If the document contains an _id field, then the save() method performs an upsert, querying the collection on the _id field. If a document does not exist with the specified _id value, the save() method performs an insert. If a document exists with the specified _id value, the save() method performs an update that replaces all fields in the existing document with the fields from the document.

  于是我猜想这个“_id"域是我们在创建对象的时候就被分配好了的,而不是在插入mongodb之后,数据库随机分配一个给我们的,于是我这样做: 

import os , string , datetime ,pymongo;
conn = pymongo.Connection("127.0.0.1",27017);
db = conn.nn;
coll = db.nn_res;
value1 = dict(name="user1",num="");
value2 = dict(name="user1",num="");
coll.save(value1);
coll.insert(value2);

  正如所料,都插入成功了。后来我又想这样会不会也都成功呢?

import os , string , datetime ,pymongo;
conn = pymongo.Connection("127.0.0.1",27017);
db = conn.nn;
coll = db.nn_res;
coll.save({"name":"user1","num":""});
coll.insert({"name":"user1","num":""});

  发现也都成功了。忽然又觉得这个跟自己所说的(”于是我猜想这个“_id"域是我们在创建对象的时候就被分配好了的,而不是在插入mongodb之后,数据库随机分配一个给我们的")有点背道而驰,但我也只好自我安慰说最后一种插入方式“_id”在{}操作的时候便已经被初始化好了。

  也许有人会说可能mongodb有一个自我hash的功能,但是也说不通。这个还真是有点让人困惑呀,我还需要再仔细想想。

关于python中使用mongodb模块,save和insert的小问题的更多相关文章

  1. Python中的random模块,来自于Capricorn的实验室

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  2. Python中的logging模块

    http://python.jobbole.com/86887/ 最近修改了项目里的logging相关功能,用到了python标准库里的logging模块,在此做一些记录.主要是从官方文档和stack ...

  3. Python中的random模块

    Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...

  4. 浅析Python中的struct模块

    最近在学习python网络编程这一块,在写简单的socket通信代码时,遇到了struct这个模块的使用,当时不太清楚这到底有和作用,后来查阅了相关资料大概了解了,在这里做一下简单的总结. 了解c语言 ...

  5. python中的StringIO模块

    python中的StringIO模块 标签:python StringIO 此模块主要用于在内存缓冲区中读写数据.模块是用类编写的,只有一个StringIO类,所以它的可用方法都在类中.此类中的大部分 ...

  6. python中的select模块

    介绍: Python中的select模块专注于I/O多路复用,提供了select  poll  epoll三个方法(其中后两个在Linux中可用,windows仅支持select),另外也提供了kqu ...

  7. Python中的re模块--正则表达式

    Python中的re模块--正则表达式 使用match从字符串开头匹配 以匹配国内手机号为例,通常手机号为11位,以1开头.大概是这样13509094747,(这个号码是我随便写的,请不要拨打),我们 ...

  8. python中的shutil模块

    目录 python中的shutil模块 目录和文件操作 归档操作 python中的shutil模块 shutil模块对文件和文件集合提供了许多高级操作,特别是提供了支持文件复制和删除的函数. 目录和文 ...

  9. Python中使用operator模块实现对象的多级排序

    Python中使用operator模块实现对象的多级排序 今天碰到一个小的排序问题,需要按嵌套对象的多个属性来排序,于是发现了Python里的operator模块和sorted函数组合可以实现这个功能 ...

随机推荐

  1. 使用highcharts 绘制Web图表

    问题描述:     使用highcharts 绘制Web图表 Highcharts说明: 问题解决:     (1)安装Highcharts     在这些图表中,数据源是一个典型的JavaScrip ...

  2. 【BZOJ】【3757】苹果树

    树分块 orz HZWER http://hzwer.com/5259.html 不知为何我原本写的倍增求LCA给WA了……学习了HZWER的倍增新姿势- 树上分块的转移看vfk博客的讲解吧……(其实 ...

  3. hibernate4.0中SessionFactory的创建

    创建SessionFactory 首先创建Configuration对象,主要方式是: new Configuration().configure() 默认情况下Hibernate会去classPat ...

  4. 【转载】CCombobox使用大全

    一.如何添加/删除Combo Box内容 1. 在Combo Box控件属性的Data标签里面添加,一行表示Combo Box下拉列表中的一行.换行用ctrl+回车. 2. 在程序初始化时动态添加 如 ...

  5. MyEclipse 简单快捷键

    1) Ctrl+/  注释当前行,再按则取消注释 2) Ctrl+M切换窗口的大小 3) Ctrl+Shift+O作用是缺少的Import语句被加入,多余的Import语句被删除. 4)Alt+/ 代 ...

  6. IOS 中的MVC设计模式

  7. hdu 1329 Hanoi Tower Troubles Again!

    找规律的题目an=an-1+(i+i%2)/2*2; ;}

  8. 通过Calendar 类获取前一个月的第一天

      SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd 00:00:00"); //获取到当前的时间     Cale ...

  9. linux进程的地址空间,核心栈,用户栈,内核线程

    linux进程的地址空间,核心栈,用户栈,内核线程 地址空间: 32位linux系统上,进程的地址空间为4G,包括1G的内核地址空间,和3G的用户地址空间. 内核栈: 进程控制块task_struct ...

  10. python 时间处理(time和datetime介绍)

    python的有关时间的有哪几种呢?今天我们介绍两个:time和datetime time模块提供各种操作时间的函数 datetime模块定义了下面这几个类: datetime.date:表示日期的类 ...