Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96830
  • 博文数量: 20
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 202
  • 用 户 组: 普通用户
  • 注册时间: 2013-09-07 01:56
个人简介

数据库技术爱好者

文章分类

全部博文(20)

文章存档

2016年(11)

2015年(9)

我的朋友

分类: NOSQL

2015-12-27 22:14:37

    好记性不如烂笔头,哪天想不起来了来看看。

点击(此处)折叠或打开

  1. #!/bin/env python

  2. from pymongo import MongoClient
  3. import random

  4. host = '192.168.1.218'
  5. port = 28017
  6. database = 'lvtu'
  7. collection = 'person'

  8. client = MongoClient(host,port)
  9. conn = client[database]
  10. #print conn[collection].find_one()

  11. nameList = ['xi dada','zhao er','zhang san','li si','wang wu']
  12. sexList = ['boy','girl']
  13. ageList = range(18,28)

  14. cityList = ['beijing','tianjin','wuhan','shanghai','hangzhou']
  15. streetList = ['1th street','2th street','3th street','4th street','5th street']

  16. personList = []
  17. tmp = 0
  18. while True:
  19.     name = nameList[random.randint(0,len(nameList)-1)] //这样写有点太二了......
  20.     sex = sexList[random.randint(0,len(sexList)-1)]
  21.     age = ageList[random.randint(0,len(ageList)-1)]
  22.     city = cityList[random.randint(0,len(cityList)-1)]
  23.     street = streetList[random.randint(0,len(streetList)-1)]
  24.     
  25.     person = {'name':name,'sex':sex,'age':age,'address':{'country':'China','city':city,'street':street}}
  26.     personList.append(person)
  27.     print person
  28.     del name,sex,age,city,street,person
  29.     tmp += 1
  30.     if tmp > 1000:
  31.         break
  32.                                         

  33. obj_ids = conn[collection].insert(personList)

  34. count = 0
  35. for id in obj_ids:
  36.     print '%s:' % count,conn[collection].find_one({"_id":id})
  37.     count += 1

阅读(1909) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~