我有像这样的子文档数组
{
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"list" : [
{
"a" : 1
},
{
"a" : 2
},
{
"a" : 3
},
{
"a" : 4
},
{
"a" : 5
}
]
}
Run Code Online (Sandbox Code Playgroud)
我可以过滤子文档> 3
我期待下面的结果
{
"_id" : ObjectId("512e28984815cbfcb21646a7"),
"list" : [
{
"a" : 4
},
{
"a" : 5
}
]
}
Run Code Online (Sandbox Code Playgroud)
我尝试使用$elemMatch
但返回数组中的第一个匹配元素
我的查询:
db.test.find( { _id" : ObjectId("512e28984815cbfcb21646a7") }, {
list: {
$elemMatch:
{ a: { $gt:3 }
}
}
} ) …
Run Code Online (Sandbox Code Playgroud) 我有一个集合Notebook,其中包含名为Notes的嵌入式数组文档.例子
文档如下所示.
{
"_id" : ObjectId("4f7ee46e08403d063ab0b4f9"),
"name" : "MongoDB",
"notes" : [
{
"title" : "Hello MongoDB",
"content" : "Hello MongoDB"
},
{
"title" : "ReplicaSet MongoDB",
"content" : "ReplicaSet MongoDB"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想找出只有标题为"Hello MongoDB"的注释.我不应该得到什么
是查询.谁能帮我.
我有像下面的mongo集合
{
"auther" : "xyz" ,
"location" : "zzz" ,
"books" :
[
{"book1" : "b1" , "date" : 2-3-00} ,
{"book1" : "b2" , "date" : 4-9-00}
]
}
{
"auther" : "pqr",
"location" : "zzz" ,
"books" :
[
{"book1" : "b1" , "date" : 2-4-00}
]
}
Run Code Online (Sandbox Code Playgroud)
我想得到书b1和作者xyz的唯一日期.
我有如下查询
db.coll.find({"auther" : "xyz" , "books.book1" : "b1"} , {"books.date" : 1})
Run Code Online (Sandbox Code Playgroud)
但它的输出如下
"books" : {"date" : 2-4-00} , "books" : {"date" : 4-9-00}
Run Code Online (Sandbox Code Playgroud)
我想只得到书b1和其他xyz .means的日期 "books" : {"date" …
我有两个域对象,
@Document
public class PracticeQuestion {
private int userId;
private List<Question> questions;
// Getters and setters
}
@Document
public class Question {
private int questionID;
private String type;
// Getters and setters
}
Run Code Online (Sandbox Code Playgroud)
我的JSON文档是这样的,
{
"_id" : ObjectId("506d9c0ce4b005cb478c2e97"),
"userId" : 1,
"questions" : [
{
"questionID" : 1,
"type" : "optional"
},
{
"questionID" : 3,
"type" : "mandatory"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我必须更新基于userId和questionId的"类型",所以我在自定义Repository接口中编写了一个findBy查询方法,
public interface CustomRepository extends MongoRepository<PracticeQuestion, String> {
List<PracticeQuestion> findByUserIdAndQuestionsQuestionID(int userId,int questionID);
}
Run Code Online (Sandbox Code Playgroud)
我的问题是当我执行此方法时userId为1而questionID为3,它返回整个问题列表而不管questionID.查询方法名称是有效的,还是应该如何为嵌套对象编写查询.
谢谢你的任何建议.
假设我在名为"users"的集合中有以下文档架构:
{
name: 'John',
items: [ {}, {}, {}, ... ]
}
Run Code Online (Sandbox Code Playgroud)
'items'数组包含以下格式的对象:
{
item_id: "1234",
name: "some item"
}
Run Code Online (Sandbox Code Playgroud)
每个用户可以在"项目"数组中嵌入多个项目.
现在,我希望能够通过item_id为给定用户获取项目.
例如,我想获取属于名为"John"的用户的ID为"1234"的项目.
我可以用mongoDB做到这一点吗?我想利用它强大的数组索引,但我不确定你是否可以在嵌入式数组上运行查询并从数组中返回对象而不是包含它的文档.
我知道我可以使用{users.items.item_id:"1234"}来获取拥有某个项目的用户.但我想从数组中获取实际项目,而不是用户.
或者,是否有更好的方法来组织这些数据,以便我可以轻松地得到我想要的东西?我仍然是mongodb的新手.
感谢您提供的任何帮助或建议.
我相信至少有两种方法可以在mongodb文档中嵌入数据.在简化的情况下,我们可以这样:
{
'name' : 'bill',
'lines': {
'idk73716': {'name': 'Line A'},
'idk51232': {'name': 'Line B'},
'idk23321': {'name': 'Line C'}
}
}
Run Code Online (Sandbox Code Playgroud)
并作为一个数组:
{
'name' : 'bill',
'lines': [
{'id': 'idk73716', 'name': 'Line A'},
{'id': 'idk51232', 'name': 'Line B'},
{'id': 'idk23321', 'name': 'Line C'}
]
}
Run Code Online (Sandbox Code Playgroud)
正如您在此用例中所看到的,保持每行的ID非常重要.
我想知道这两种模式之间是否有利弊.特别是当涉及到使用索引时,我感觉第二个可能更容易使用,因为可以在'lines.id'或甚至'lines.name'上创建索引来搜索所有文档中的id或名称.我没有找到任何工作解决方案来索引第一个例子中的id('idk73716'等).
如果您有这样的用例,通常首选使用第二种方法吗?
我收集了产品.每个产品都包含一系列项目.
> db.products.find().pretty()
{
"_id" : ObjectId("54023e8bcef998273f36041d"),
"shop" : "shop1",
"name" : "product1",
"items" : [
{
"date" : "01.02.2100",
"purchasePrice" : 1,
"sellingPrice" : 10,
"count" : 15
},
{
"date" : "31.08.2014",
"purchasePrice" : 10,
"sellingPrice" : 1,
"count" : 5
}
]
}
Run Code Online (Sandbox Code Playgroud)
那么,请你给我一个建议,如何查询MongoDB以检索所有只有单个项目的产品,其中日期等于我传递给查询作为参数的日期.
"31.08.2014"的结果必须是:
{
"_id" : ObjectId("54023e8bcef998273f36041d"),
"shop" : "shop1",
"name" : "product1",
"items" : [
{
"date" : "31.08.2014",
"purchasePrice" : 10,
"sellingPrice" : 1,
"count" : 5
}
]
}
Run Code Online (Sandbox Code Playgroud) 在我的mongoDB书籍集中,我的文档结构如下:
/* 0 */
{
"_id" : ObjectId("50485b89b30f1ea69110ff4c"),
"publisher" : {
"$ref" : "boohya",
"$id" : "foo"
},
"displayName" : "Paris Nightlife",
"catalogDescription" : "Some desc goes here",
"languageCode" : "en",
"rating" : 0,
"status" : "LIVE",
"thumbnailId" : ObjectId("50485b89b30f1ea69110ff4b"),
"indexTokens" : ["Nightlife", "Paris"]
}
Run Code Online (Sandbox Code Playgroud)
我执行以下正则表达式查询以查找具有以"Par"开头的一个indexToken的所有文档:
{ "indexTokens" : { "$regex" : "^Par" , "$options" : "i"}}
Run Code Online (Sandbox Code Playgroud)
如果我只选择要返回的indexTokens字段,如下所示:
{ "indexTokens" : 1}
Run Code Online (Sandbox Code Playgroud)
生成的DBObject是
{ "_id" : { "$oid" : "50485b89b30f1ea69110ff4c"} , "indexTokens" : [ "Nightlife" , "Paris"]}
Run Code Online (Sandbox Code Playgroud)
我想得到的只是与正则表达式匹配的标记/标记(此时我不关心检索文档,我也不需要匹配文档的所有标记)
这是MongoDB v2.2下新的聚合框架的案例吗?? …
db.users.find({})返回具有所有字段的所有用户.
如何编写一个只返回所有用户的"电子邮件"字段的请求?
如何从具有以下结构的 Mongo 文档中的数组中获取一个元素:
{
array : [
{type: 'cat', name: 'George'}
{type: 'cat', name: 'Mary'}
{type: 'dog', name: 'Steve'}
{type: 'dog', name: 'Anna'}
]
}
Run Code Online (Sandbox Code Playgroud)
例如,我需要得到 Steve,在这种情况下,结果必须如下所示:
{
array : [
{type: 'dog', name: 'Steve'}
]
}
Run Code Online (Sandbox Code Playgroud)
或者: {type: 'dog', name: 'Steve'}
我知道如何在发布时制作它,但我需要在整个数组可用的客户端制作它,我可以使用 forEach 从数组中返回这个值,但我正在寻找更优雅的方式(使用 Mongo 查询)。
mongodb ×10
arrays ×4
collections ×1
filter ×1
findby ×1
meteor ×1
minimongo ×1
spring-data ×1