我正在尝试学习 Mongodb 中的索引。我创建了一个数据库和一个集合,如下所示:
use mydb
db.createCollection("myFirstCollection")
one={name:"Helios"}
two={name:"Kepler"}
db.myFirstCollection.insert(one)
db.myFirstCollection.insert(two)
Run Code Online (Sandbox Code Playgroud)
我能够列出我的结果如下:
db.myFirstCollection.find()
{ "_id" : ObjectId("53cde256f8807057b6bd827b"), "name" : "Helios" }
{ "_id" : ObjectId("53cde25bf8807057b6bd827c"), "name" : "kepler" }
Run Code Online (Sandbox Code Playgroud)
我想在该字段上添加唯一索引name。但是当我尝试时,出现以下错误
db.myFirstCollection.ensureIndex({name:1},{unique:true})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"ok" : 0,
"errmsg" : "E11000 duplicate key error index: mydb.myFirstCollection.$name_1 dup key: { : null }",
"code" : 11000
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚我所犯的错误。请帮忙。
我有一个简单的Spring MVC应用程序,它带有一个jsp和一个控制器类,部署在一个tomcat服务器中.该设置适用于多个请求.我将控制器类命名为com.mypackage.mvcController.
现在我使用jvisualvm来查找创建此特定控制器类的实例数.它显示2.
这是我的配置:web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/pages/welcome.jsp</welcome-file>
</welcome-file-list>
</web-app>
Run Code Online (Sandbox Code Playgroud)
mvc-dispatcher-servlet.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.myPackage" />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
Run Code Online (Sandbox Code Playgroud)
和项目结构:

控制器类:
package com.myPackage;
import …Run Code Online (Sandbox Code Playgroud)