由于mongo是一个树状结构数据存储,所以我们往往需要基于子文档去查询某个东西,比如数据如下:
"_id"
:
"97d73f8dd62a4c659c37335e321b8cc8"
,
"end_time"
:
ISODate
(
"2020-12-01T00:00:00.000+08:00"
)
,
"start_time"
:
ISODate
(
"2020-11-01T14:00:00.000+08:00"
)
,
"week_cfg"
:
[
NumberInt
(
0
)
,
NumberInt
(
1
)
,
NumberInt
(
2
)
,
NumberInt
(
3
)
,
NumberInt
(
4
)
,
NumberInt
(
5
)
"packages"
:
[
"price"
:
NumberLong
(
3000000
)
,
"awards"
:
[
"_id"
:
NumberInt
(
0
)
,
"type"
:
"COIN"
,
"desc"
:
"分贝"
,
"num"
:
NumberInt
(
10
)
"_id"
:
NumberInt
(
1
)
,
"type"
:
"GIFT"
,
"desc"
:
"彩虹糖"
,
"num"
:
NumberInt
(
10
)
"_id"
:
NumberInt
(
3
)
,
"type"
:
"GIFT"
,
"desc"
:
"尴尬"
,
"num"
:
NumberInt
(
10
)
"price"
:
NumberLong
(
5000000
)
,
"awards"
:
[
"_id"
:
NumberInt
(
1
)
,
"type"
:
"FRAME"
,
"desc"
:
"来吼鸭"
,
"num"
:
NumberInt
(
10
)
"_id"
:
NumberInt
(
1
)
,
"type"
:
"HOME_FLOAT"
,
"desc"
:
"流星"
,
"num"
:
NumberInt
(
10
)
"online"
:
true
,
"time"
:
ISODate
(
"2020-11-02T10:51:34.866+08:00"
)
"_id"
:
"de0a1b4df7e84f288eab663ea28b0aa7"
,
"end_time"
:
ISODate
(
"2001-11-30T00:00:00.000+08:00"
)
,
"start_time"
:
ISODate
(
"2001-11-02T00:00:00.000+08:00"
)
,
"week_cfg"
:
[
NumberInt
(
2
)
"packages"
:
[
"price"
:
NumberLong
(
9000000
)
,
"awards"
:
[
"_id"
:
NumberInt
(
0
)
,
"type"
:
"COIN"
,
"num"
:
NumberInt
(
100
)
"_id"
:
NumberInt
(
1
)
,
"type"
:
"ROOM_SPEECH_BUBBLE"
,
"num"
:
NumberInt
(
100
)
"_id"
:
NumberInt
(
1
)
,
"type"
:
"HOME_FLOAT"
,
"num"
:
NumberInt
(
100
)
"price"
:
NumberLong
(
1000000
)
,
"awards"
:
[
"_id"
:
NumberInt
(
0
)
,
"type"
:
"COIN"
,
"num"
:
NumberInt
(
100
)
"_id"
:
NumberInt
(
1
)
,
"type"
:
"GIFT"
,
"num"
:
NumberInt
(
100
)
"_id"
:
NumberInt
(
1
)
,
"type"
:
"HOME_FLOAT"
,
"num"
:
NumberInt
(
100
)
"online"
:
false
,
"time"
:
ISODate
(
"2020-10-31T14:51:56.874+08:00"
)
如果我们希望查询week_cfg这个列表中有0的文档:
db.exchange_package_cfg.find({"week_cfg":0})
.projection({})
.sort({_id:-1})
.limit(100)
如果希望查询week_cfg这个类表存在0,1,2某一个的文档:
db.exchange_package_cfg.find({"week_cfg":{$in:[0,1,2]}})
.projection({})
.sort({_id:-1})
.limit(100)
如果我们需要查询packages下的awards列表中,type字段为COIN的文档:
db.exchange_package_cfg.find({"packages.awards.type":"COIN"})
.projection({})
.sort({_id:-1})
.limit(100)
如果我们需要查询packages下的awards列表中,type字段为COIN或者GIFT的文档:
db.exchange_package_cfg.find({"packages.awards.type":{$in:["COIN","GIFT"]}})
.projection({})
.sort({_id:-1})
.limit(100)
由于mongo是一个树状结构数据存储,所以我们往往需要基于子文档去查询某个东西,比如数据如下:{ "_id" : "97d73f8dd62a4c659c37335e321b8cc8", "end_time" : ISODate("2020-12-01T00:00:00.000+08:00"), "start_time" : ISODate("2020-11-01T14:00:00.000+08:00"), "week_cfg" : [ NumberInt(0), NumberInt
mongodb 子文档查询 此博文包含图片 (2012-12-10 18:03:22)转载▼
标签: it 分类: mongoDB
首先我的数据库里有两个文档,每个文档里都有2个嵌套的数组:mongodb 子文档查询
如果我想查询comments里score大于5的记录:
testProvider.find({"comments.score":{"$gt":5}},{},function
"_id": "6220a1942fefba0066659",
"formDataId": "d885b3094c24160b3756997d59fe5175",
"formStructId": "cfed43eb74bab08ca36eb8343319d98e",
"qrCodeId": "2a445726c47ed0b344a0546abf09ac00",
"data": {
"list": [
MongoDB不支持子查询,碰到这些复杂的运算就只能先将数据读出后再计算,而用Java等语言编写这类计算也不是很简单,这时可以考虑用esProc辅助实现。下面我们通过一个例子来看一下具体做法。
MongoDB中的文档orders保存了订单数据,employee保存了员工数据。如下:
> db.orders.find();
{ “_id” : ObjectId(“543
MongoDB在某些方面确实比关系型数据库更强 (比如对追加型日志数据的吞吐能力),但结构化计算能力方面较弱。比如:MongoDB不支持子查询,碰到这些复杂的运算就只能先将数据读出后再计算,而用Java等语言编写这类计算也不是很简单。
比如要处理这么个场景:查出订单信息,要求订单中的SELLERID必须是employee集合中STATE= California的员工id。如果写成sql就是:
Select * from orders where orders.sellerid in (select ei
对mongodb 字段中的列表进行查询统计,在日常过程中,需要对mongdb 的字段中为列表的数据进行查询 。mongodb 字段类型为list 中的包含的数据进行统计。
"_id" : "bd2984a2246af9245c22db1fe7511d80",
"CorrectwordAll" : [
"word" : "下功夫(工)",
"counts" : 44
```java
MongoClient mongoClient = new MongoClient("localhost", 27017);
MongoDatabase database = mongoClient.getDatabase("yourDatabaseName");
MongoCollection<Document> collection = database.getCollection("yourCollectionName");
long count = collection.countDocuments();
System.out.println("Number of documents in the collection: " + count);
这将连接到名为"yourDatabaseName"的MongoDB数据库,并查询名为"yourCollectionName"的集合中的文档数。countDocuments()方法返回集合中文档的数量。