MongoDB 数组查询(数组中符合条件返回)+分页+排序

这篇博客展示了如何在 MongoDB 中进行数据插入和查询操作。首先,通过 `db.arrayquery.insert()` 创建了包含多个文档的数据集合,每个文档包含了 `name`、`creator` 和 `contentList` 等字段。接着,使用聚合框架 `$match`、`$project`、`$sort` 和 `$limit` 操作符,筛选出 `contentList` 中 `name` 为 '皇冠梨' 的记录,并按 `name` 倒序排列,最后返回了符合条件的前三个文档。此外,还展示了如何利用 `$and` 和 `$or` 操作符进行复杂查询,结合 `customer` 进行筛选。
数据创建
db.arrayquery.insert([
{
	"_id" : NumberLong(3),
	"name" : "n1",
	"creator" : "c3",
	"contentList" : [
		{
			"name" : "皇冠梨3",
			"hover" : 20,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "皇冠梨",
				"type" : 2
			}
		},
		{
			"name" : "模板2",
			"hover" : 10,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "模板2",
				"type" : 2
			}
		},
		{
			"name" : "10寸西红柿",
			"hover" : 13,
			"content" : {
				"source" : "s2",
				"mId" : "m0001",
				"name" : "10寸西红柿",
				"type" : 2
			}
		}
	],
	"gmtCreate" : NumberLong("2718064537"),
	"gmtModified" : NumberLong("2718064537"),
	"serial" : 1,
	"priority" : 20
},

{
	"_id" : NumberLong(2),
	"name" : "n1",
	"creator" : "c2",
	"contentList" : [
		{
			"name" : "皇冠梨",
			"hover" : 20,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "皇冠梨",
				"type" : 2
			}
		},
		{
			"name" : "模板2",
			"hover" : 10,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "模板2",
				"type" : 2
			}
		},
		{
			"name" : "10寸西红柿",
			"hover" : 13,
			"content" : {
				"source" : "s2",
				"mId" : "m0001",
				"name" : "10寸西红柿",
				"type" : 2
			}
		}
	],
	"gmtCreate" : NumberLong("2718064537"),
	"gmtModified" : NumberLong("2718064537"),
	"serial" : 1,
	"priority" : 20
},

{
	"_id" : NumberLong(1),
	"name" : "n1",
	"creator" : "c1",
	"contentList" : [
		{
			"name" : "皇冠梨",
			"hover" : 20,
			"content" : {
				"source" : "s1",
				"mId" : "m0001",
				"name" : "皇冠梨"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值