java mongodb开发_Java Tutorial:Java操作MongoDB入门

 2023-09-08 阅读 27 评论 0

摘要:【IT168 技术】个人编译能力有限,以下提供英汉对照,欢迎讨论指正。Introduction介绍MongoDb、This page is a brief overview of working with the MongoDB Java Driver.这是使用MongoDB java驱动的简单说明。For more information about the Java API, please

【IT168 技术】个人编译能力有限,以下提供英汉对照,欢迎讨论指正。

Introduction

介绍

MongoDb、This page is a brief overview of working with the MongoDB Java Driver.

这是使用MongoDB java驱动的简单说明。

For more information about the Java API, please refer to the online API Documentation for Java Driver

想获取更多关于java的API,请查看在线API文档。

mongodb使用教程?A Quick Tour

Using the Java driver is very simple. First, be sure to include the driver jar mongo.jar in your classpath. The following code snippets come from the examples/QuickTour.java example code found in the driver.

使用很简单。首先将驱动mongo.jar放入classpath。下面的代码段是驱动中例子examples/QuickTour.java中的内容

Making A Connection

docker mongodb。创建连接

To make a connection to a MongoDB, you need to have at the minimum, the name of a database to connect to. The database doesn't have to exist - if it doesn't, MongoDB will create it for you.

创建连接至少需要你要连接的数据库名。如果数据库不存在,MongoDB会自动创建。

Additionally, you can specify the server address and port when connecting. The following example shows three ways to connect to the database mydb on the local machine :

mybatis mongodb,此外,你还可以指定数据库服务器地址和端口。下边的例子中有三种连接本机mydb数据库方法:

importcom.mongodb.Mongo;importcom.mongodb.DB;importcom.mongodb.DBCollection;importcom.mongodb.BasicDBObject;importcom.mongodb.DBObject;importcom.mongodb.DBCursor;

Mongo m=newMongo();//orMongo m=newMongo("localhost");//orMongo m=newMongo("localhost",27017);

DB db=m.getDB("mydb");

java session。At this point, the db object will be a connection to a MongoDB server for the specified database. With it, you can do further operations.

db对象就是连接服务器中指定数据库的连接。使用他你可以做很多操作。

Note: The Mongo object instance actually represents a pool of connections to the database; you will only need one object of class Mongo even with multiple threads. See the concurrency doc page for more information.

注意:Mongo的实例是数据库连接池;在多个线程中只需要一个实例。更多的介绍请参考concurrency文档。

django mongodb?The Mongo class is designed to be thread safe and shared among threads. Typically you create only 1 instance for a given DB cluster and use it across your app. If for some reason you decide to create many mongo intances, note that:

Mongo类是线程安全和共享的。可以在整个应用中使用他。如果你想创建多个Mongo实例,注意:

all resource usage limits (max connections, etc) apply per mongo instance.

每个mongo实例的资源使用限制。

mongodb怎么用、to dispose of an instance, make sure you call mongo.close() to clean up resources.

记得使用mongo.close()关闭资源。

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/19418.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息