Java操作mongodb的歪曲查询和标准查询,python连接

作者: 韦德国际1946手机版  发布:2019-05-28

本文实例讲述了Python操作mongodb数据库举行模糊查询操作。分享给大家供我们参照他事他说加以调查,具体如下:

# -*- coding: utf-8 -*-
import pymongo
import re
from pymongo import MongoClient

本意是想查看mongo数据库的int类型的like怎么查,可是好像没 化解那么些标题。

一、相关代码
数据库配置类 MongoDBConn.py

# -*- coding: utf-8 -*-
import pymongo
import re
from pymongo import MongoClient
#创建连接
#10.20.66.106
client = MongoClient('10.20.4.79', 27017)
#client = MongoClient('10.20.66.106', 27017)
db_name = 'ta'
db = client[db_name]

 

正确查询;模糊查询;分页查询,每页多少:按某些字段排序(或升或降):查询数量:大于,小于,等于;且,或,某些字段不为空,有些字段不存在,查询在某些范围内,删除等等查询。

复制代码 代码如下:

假诺mongodb数据库中school 集结中有局部数量记录

#创设连接
#10.20.66.106
client = MongoClient('10.20.4.79', 27017)
#client = MongoClient('10.20.66.106', 27017)
db_name = 'ta'
db = client[db_name]

①. 常用查询:

#encoding=utf-8
'''

{ "_id" : 1, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 2, "zipcode" : "63110", "students" : { "comments" : "python abc" } }
{ "_id" : 3, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 4, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 5, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 7, "zipcode" : "63109", "students" : { "comments" : "python abc" }, "school" : "102 python abc" }
{ "_id" : 8, "zipcode" : "63109", "students" : { "comments" : "python abc" }, "school" : "100 python abc xyz" }
{ "_id" : 9, "zipcode" : "100", "students" : { "name" : "mike", "age" : 12, "comments" : "python" } }
{ "_id" : 10, "zipcode" : "100", "students" : { "name" : "Marry", "age" : 42, "comments" : "this is a python" } }
{ "_id" : 11, "zipcode" : "100", "students" : { "name" : "joe", "age" : 92, "comments" : "this is a python program" } }
{ "_id" : 12, "zipcode" : "100", "students" : { "name" : "joedd", "age" : 34, "comments" : "python is a script language" } }

比方mongodb数据库中school 集合中有1部分数量记录

  1. Java操作mongodb的歪曲查询和标准查询,python连接mongodb操作数据示例。询问一条数据:(多用于保存时判别db中是或不是已有眼下数据,这里 is  精确相称,模糊相配 使用regex...)

Mongo Conn连接类
'''

现在要对students中comments的多少开始展览模糊查询, python中模糊查询要依赖正则表明式:

{ "_id" : 1, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 2, "zipcode" : "63110", "students" : { "comments" : "python abc" } }
{ "_id" : 3, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 4, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 5, "zipcode" : "63109", "students" : { "comments" : "python abc" } }
{ "_id" : 7, "zipcode" : "63109", "students" : { "comments" : "python abc" }, "school" : "102 python abc" }
{ "_id" : 8, "zipcode" : "63109", "students" : { "comments" : "python abc" }, "school" : "100 python abc xyz" }
{ "_id" : 9, "zipcode" : "100", "students" : { "name" : "mike", "age" : 12, "comments" : "python" } }
{ "_id" : 10, "zipcode" : "100", "students" : { "name" : "Marry", "age" : 42, "comments" : "this is a python" } }
{ "_id" : 11, "zipcode" : "100", "students" : { "name" : "joe", "age" : 92, "comments" : "this is a python program" } }
{ "_id" : 12, "zipcode" : "100", "students" : { "name" : "joedd", "age" : 34, "comments" : "python is a script language" } }

public PageUrl getByUrl(String url) { 
  return findOne(new Query(Criteria.where("url").is(url)),PageUrl.class); 
 } 

import pymongo

1、查询comments中包含"abc"的记录:

这段日子要对students中comments的数目进行模糊查询, python中模糊查询要信赖正则表明式:

  1. 询问多条数据:linkUrl.id 属于个别查询

class DBConn:
    conn = None
    servers = "mongodb://localhost:27017"

for u in db.school.find({'students.comments':re.compile('abc')}):
  print u

1、查询comments中包含"abc"的记录:

public List<PageUrl> getPageUrlsByUrl(int begin, int end,String linkUrlid) {   
  Query query = new Query(); 
  query.addCriteria(Criteria.where("linkUrl.id").is(linkUrlid)); 
  return find(query.limit(end - begin).skip(begin), PageUrl.class);   
 } 

    def connect(self):
        self.conn = pymongo.Connection(self.servers)

结果如下:

for u in db.school.find({'students.comments':re.compile('abc')}):
  print u

叁.歪曲查询:-----人命关天字---regex

    def close(self):
        return self.conn.disconnect()

{u'students': {u'comments': u'python abc'}, u'_id': 1.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 2.0, u'zipcode': u'63110'}
{u'students': {u'comments': u'python abc'}, u'_id': 3.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 4.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 5.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'school': u'102 python abc', u'_id': 7.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'school': u'100 python abc xyz', u'_id': 8.0, u'zipcode': u'63109'}

结果如下:

public long getProcessLandLogsCount(List<Condition> conditions) 
 { 
  Query query = new Query(); 
  if (conditions != null && conditions.size() > 0) { 
   for (Condition condition : conditions) { 
    query.addCriteria(Criteria.where(condition.getKey()).regex(".*?\"  condition.getValue().toString()  ".*")); 
   } 
  } 
  return count(query, ProcessLandLog.class); 
 } 

    def getConn(self):
        return self.conn

2、查询comments中包含"this is"的记录:

{u'students': {u'comments': u'python abc'}, u'_id': 1.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 2.0, u'zipcode': u'63110'}
{u'students': {u'comments': u'python abc'}, u'_id': 3.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 4.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'_id': 5.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'school': u'102 python abc', u'_id': 7.0, u'zipcode': u'63109'}
{u'students': {u'comments': u'python abc'}, u'school': u'100 python abc xyz', u'_id': 8.0, u'zipcode': u'63109'}

最上边,小编在代码亲自实践过的混淆查询,只援助字段属性是字符串的查询,你倘使查字段属性是int的歪曲查询,还真没辙。

MongoDemo.py 类

for u in db.school.find({'students.comments':re.compile('this is')}):
  print u

2、查询comments中包含"this is"的记录:

四.gte: 大于等于,lte小于等于...注意查询的时候各样字段的花色要和mongodb中数据类型一致

复制代码 代码如下:

结果如下:

for u in db.school.find({'students.comments':re.compile('this is')}):
  print u

public List<ProcessLandLog> getProcessLandLogs(int begin,int end,List<Condition> conditions,String orderField,Direction direction) 
 { 
  Query query = new Query(); 
  if (conditions != null && conditions.size() > 0) { 
   for (Condition condition : conditions) { 
    if(condition.getKey().equals("time")){ 
     query.addCriteria(Criteria.where("time").gte(condition.getValue())); //gte: 大于等于 
    }else if(condition.getKey().equals("insertTime")){ 
     query.addCriteria(Criteria.where("insertTime").gte(condition.getValue())); 
    }else{ 
     query.addCriteria(Criteria.where(condition.getKey()).is(condition.getValue())); 
    } 
   } 
  } 
  return find(query.limit(end - begin).skip(begin).with(new Sort(new Sort.Order(direction, orderField))), ProcessLandLog.class); 
 } 
public List<DpsLand> getDpsLandsByTime(int begin, int end, Date beginDate,Date endDate) { 
 return find(new Query(Criteria.where("updateTime").gte(beginDate).lte(endDate)).limit(end - begin).skip(begin), 
 DpsLand.class); 
 } 

#encoding=utf-8
'''

{u'students': {u'age': 42.0, u'name': u'Marry', u'comments': u'this is a python'}, u'_id': 10.0, u'zipcode': u'100'}
{u'students': {u'age': 92.0, u'name': u'joe', u'comments': u'this is a python program'}, u'_id': 11.0, u'zipcode': u'100'}

结果如下:

查询字段不存在的数额 -----关键字---not

Mongo操作Demo
Done:
'''
import MongoDBConn

有鉴于此,模糊查询要用到re模块,查询条件利用re.compile()函数

{u'students': {u'age': 42.0, u'name': u'Marry', u'comments': u'this is a python'}, u'_id': 10.0, u'zipcode': u'100'}
{u'students': {u'age': 92.0, u'name': u'joe', u'comments': u'this is a python program'}, u'_id': 11.0, u'zipcode': u'100'}

public List<GoodsDetail> getGoodsDetails2(int begin, int end) { 
  Query query = new Query(); 
  query.addCriteria(Criteria.where("goodsSummary").not()); 
  return find(query.limit(end - begin).skip(begin),GoodsDetail.class); 
 } 

dbconn = MongoDBConn.DBConn()
conn = None
lifeba_users = None

越来越多关于Python相关内容感兴趣的读者可查看本站专项论题:《Python常见数据库操作技术汇总》、《Python数学运算技能统计》、《Python数据结构与算法教程》、《Python函数使用技能计算》、《Python字符串操作本领汇总》、《Python入门与进级优异教程》及《Python文件与目录操作本领汇总》

 

查询字段不为空的多寡     -----关键字---ne

def process():
    #树立连接
    dbconn.connect()
    global conn
    conn = dbconn.getConn()

本文由韦德国际1946发布于韦德国际1946手机版,转载请注明出处:Java操作mongodb的歪曲查询和标准查询,python连接

关键词: python MongoDB 新伟