当前位置: 首页 > news >正文

深圳企业公司做网站新闻头条今天最新消息

深圳企业公司做网站,新闻头条今天最新消息,免费推广工具有哪些,c2c网站管理系统下载文章目录 1.引入Java驱动依赖2.快速开始2.1 先在monsh连接建立collection2.2 java中快速开始2.3 Insert a Document2.4 Update a Document2.5 Find a Document2.6 Delete a Document 1.引入Java驱动依赖 注意&#xff1a;启动服务的时候需要加ip绑定 需要引入依赖 <dependen…

文章目录

  • 1.引入Java驱动依赖
  • 2.快速开始
    • 2.1 先在monsh连接建立collection
    • 2.2 java中快速开始
    • 2.3 Insert a Document
    • 2.4 Update a Document
    • 2.5 Find a Document
    • 2.6 Delete a Document

1.引入Java驱动依赖

注意:启动服务的时候需要加ip绑定
需要引入依赖

<dependency><groupId>org.mongodb</groupId><artifactId>mongodb-driver-sync</artifactId><version>5.1.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.mongodb/bson --><dependency><groupId>org.mongodb</groupId><artifactId>bson</artifactId><version>5.1.0</version></dependency><!-- https://mvnrepository.com/artifact/org.mongodb/mongodb-driver-core --><dependency><groupId>org.mongodb</groupId><artifactId>mongodb-driver-core</artifactId><version>5.1.0</version></dependency>

2.快速开始

2.1 先在monsh连接建立collection

db.players.insertOne({name:"palyer1",height:181,weigh:90})
{acknowledged: true,insertedId: ObjectId('665c5e0a522ef6dba6a26a13')
}
test> db.players.insertOne({name:"palyer2",height:190,weigh:100})
{acknowledged: true,insertedId: ObjectId('665c5e18522ef6dba6a26a14')
}
test> db.players.find()
[{_id: ObjectId('665c5e0a522ef6dba6a26a13'),name: 'player1',height: 181,weigh: 90},{_id: ObjectId('665c5e18522ef6dba6a26a14'),name: 'player2',height: 190,weigh: 100}
]

2.2 java中快速开始

public class QuickStart {public static void main(String[] args) {// 连接的urlString uri = "mongodb://node02:27017";try (MongoClient mongoClient = MongoClients.create(uri)) {MongoDatabase database = mongoClient.getDatabase("test");MongoCollection<Document> collection = database.getCollection("players");Document doc = collection.find(eq("name", "palyer2")).first();if (doc != null) {//{"_id": {"$oid": "665c5e18522ef6dba6a26a14"}, "name": "palyer2", "height": 190, "weigh": 100}System.out.println(doc.toJson());} else {System.out.println("No matching documents found.");}}}}

2.3 Insert a Document

 @Testpublic void InsertDocument(){MongoDatabase database = mongoClient.getDatabase("test");MongoCollection<Document> collection = database.getCollection("map");try {// 插入地图文档InsertOneResult result = collection.insertOne(new Document().append("_id", new ObjectId()).append("name", "beijing").append("longitude", "40°N").append("latitude", "116°E"));//打印文档的id//Success! Inserted document id: BsonObjectId{value=665c6424a080bb466d32e645}System.out.println("Success! Inserted document id: " + result.getInsertedId());// Prints a message if any exceptions occur during the operation} catch (MongoException me) {System.err.println("Unable to insert due to an error: " + me);}

2.4 Update a Document

 @Testpublic void UpdateDocument(){MongoDatabase database = mongoClient.getDatabase("test");MongoCollection<Document> collection = database.getCollection("map");//构造更新条件Document query = new Document().append("name",  "beijing");// 指定更新的字段Bson updates = Updates.combine(Updates.set("longitude", "40.0N"),Updates.set("latitude", "116.0E"));// Instructs the driver to insert a new document if none match the queryUpdateOptions options = new UpdateOptions().upsert(true);try {// 更新文档UpdateResult result = collection.updateOne(query, updates, options);// Prints the number of updated documents and the upserted document ID, if an upsert was performedSystem.out.println("Modified document count: " + result.getModifiedCount());System.out.println("Upserted id: " + result.getUpsertedId());} catch (MongoException me) {System.err.println("Unable to update due to an error: " + me);}}

2.5 Find a Document

 @Testpublic void testFindDocuments(){MongoDatabase database = mongoClient.getDatabase("test");MongoCollection<Document> collection = database.getCollection("map");// 创建检索条件/*Bson projectionFields = Projections.fields(Projections.include("name", "shanghai"),Projections.excludeId());*/// 匹配过滤检索文档MongoCursor<Document> cursor = collection.find(eq("name", "shanghai"))//.projection(projectionFields).sort(Sorts.descending("longitude")).iterator();// 打印检索到的文档try {while(cursor.hasNext()) {System.out.println(cursor.next().toJson());}} finally {cursor.close();}}

检索

2.6 Delete a Document

 @Testpublic void DeleteDocument(){MongoDatabase database = mongoClient.getDatabase("test");MongoCollection<Document> collection = database.getCollection("map");Bson query = eq("name", "shanghai");try {// 删除名字为shanghai的地图DeleteResult result = collection.deleteOne(query);System.out.println("Deleted document count: " + result.getDeletedCount());// 打印异常消息} catch (MongoException me) {System.err.println("Unable to delete due to an error: " + me);}}
http://www.ds6.com.cn/news/67928.html

相关文章:

  • 怎么制作公司自己网站福建seo网站
  • 推广型网站如何建站java培训机构十强
  • 未来中森网站建设注册域名查询网站官网
  • 江门网站建设无线网络优化工程师
  • 广告公司宣传册样本网站推广优化公司
  • 自助建站的优势百度官方营销推广平台
  • 宣传型网站建设友链交易平台
  • 手机网站建设找哪家二级域名网站查询入口
  • 为什么很少用python做网站网络营销专业就业公司
  • 丰县网站建设推广如何在各大网站发布信息
  • 济南传承网站建设公司免费做网站网站
  • 公司做网站的费用怎么做账公司推广策划方案
  • 广州市手机网站建设公司东莞网站推广技巧
  • 网页设计与网站建设课程竞价推广账户竞价托管收费
  • 网站建设的内容有哪些公司官网开发制作
  • 东莞北京网站建设价格大数据是干什么的
  • 做旅游信息的网站能赚钱吗凡科建站代理
  • wordpress 复制网站搜索引擎排名优化程序
  • 中国空间站距离地面多少公里谷歌浏览器网页
  • 网站建设的功能需求分析策划书站长之家seo一点询
  • 互联网整合营销推广百度seo点击工具
  • 建设可以聊天的网站找片子有什么好的关键词
  • 网站自己做还是找公司求几个微信推广平台
  • 35开始学网站开发中山疫情最新消息
  • 加强公司网站建设及数据库的通知百度官方下载
  • 成都营销网站制作什么是搜索引擎营销?
  • 生态建筑建设公司网站qq推广平台
  • php 网站开发平台百度网盘客服
  • 2021重庆互联网公司排名广东网站seo营销
  • 网站推广的网站作用品牌营销策略论文