在文档中搜索值 [MongoDB]
Search for a value inside a document [MongoDB]
我正在尝试在 MongoDB 中的文档中查找值,编码为 JAVA ,我举个例子:
主要文档:
date: 12.2
rate: 9
people:
1:George
2:Candice
3: James
4: John
x:
x+1:
This is really just in theory, the date and rate don't matter it's
just an example.
假设我有 people 列,其中有一个包含 1,2,3,4,x... 无限的新文档,如果没有在 MongoDB?
中指定数字
你可以试试这个https://docs.mongodb.com/manual/tutorial/query-arrays/
或者您可以在 java 中使用 MongoTemplate 并使用 find 方法获取您案例中的人员列表。
我看到的是人是一张地图所以你可以使用
db.collection_name.findOne({"people":{$regex:".*James.*"}});
这将根据正则表达式中的名称获取值
这是使用java
Query query = new Query();
query.addCriteria(Criteria.where("people").regex(".*James.*"));
List<User> userTest = mongoOperation.find(query, User.class);// User.class is the collection-name
System.out.println("query - " + query.toString());
for (User user : userTest) {
System.out.println("userTest - " + user);
}
Output
query - Query: { "people" : { "$regex" : ".*James.*"}}, Fields: null, Sort: null
userTest - You will get the collection in array
希望这对你有用。
我正在尝试在 MongoDB 中的文档中查找值,编码为 JAVA ,我举个例子:
主要文档:
date: 12.2
rate: 9
people:
1:George
2:Candice
3: James
4: John
x:
x+1:
This is really just in theory, the date and rate don't matter it's just an example.
假设我有 people 列,其中有一个包含 1,2,3,4,x... 无限的新文档,如果没有在 MongoDB?
中指定数字你可以试试这个https://docs.mongodb.com/manual/tutorial/query-arrays/ 或者您可以在 java 中使用 MongoTemplate 并使用 find 方法获取您案例中的人员列表。
我看到的是人是一张地图所以你可以使用
db.collection_name.findOne({"people":{$regex:".*James.*"}});
这将根据正则表达式中的名称获取值
这是使用java
Query query = new Query();
query.addCriteria(Criteria.where("people").regex(".*James.*"));
List<User> userTest = mongoOperation.find(query, User.class);// User.class is the collection-name
System.out.println("query - " + query.toString());
for (User user : userTest) {
System.out.println("userTest - " + user);
}
Output
query - Query: { "people" : { "$regex" : ".*James.*"}}, Fields: null, Sort: null
userTest - You will get the collection in array
希望这对你有用。