mymongo 基础操作

前端之家收集整理的这篇文章主要介绍了mymongo 基础操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

插入数据

import datetime
import pymongo
from pymongo import MongoClient

mclient = MongoClient('192.168.3.110', 27017, connect=False)
mdb = mclient.test_database

posts=mdb.posts

new_posts = [{"author": "Mike",              "text": "Another post!",              "tags": ["bulk", "insert"],              "date": datetime.datetime(2009, 11, 12, 14)},             {"author": "Eliot",              "title": "MongoDB is fun",              "text": "and pretty easy too!", 10, 45)}]
              
result = posts.insert_many(new_posts)
print(posts.count_documents({}))

创建索引

import pymongo
from pymongo import MongoClient

mclient = MongoClient('192.168.3.110', connect=False)
mdb = mclient.test_database

posts=mdb.posts

posts.create_index([('author', 1)],)

获取当前集合的索引字段

posts=mdb.posts

posts.index_information()

删除索引

posts=mdb.posts

posts.drop_index('authorIdx')
or
posts.drop_index({ "author" : 1 })

https://docs.mongodb.com/manual/reference/method/db.collection.dropIndex/index.html

猜你在找的MongoDB相关文章