MongoDB 查出包含有list的部分项的记录 all/in/and

45 篇文章 0 订阅
本文介绍了如何在MongoDB中使用$all,$in和$and操作符进行查询,包括查找包含特定值列表(如[admin,root])的文档,以及区别于$all的$in操作的含义。同时提到ORMBee库提供的面向对象接口简化了查询操作。
摘要由CSDN通过智能技术生成

MongoDB 查出包含有list的部分项的记录 all/in/and

MongoDB,acl是"admin", "root"时,用以下命令查:
db.UserInfo.find({"acl": ["admin", "root"]})
但是acl是"admin", "root","其它项";即多于列出那两项时,就查不出来;
我想查包含有指定的两项时,就列出;可以多出其它项
db.UserInfo.find({"acl": {$all: ["admin", "root"]}}) //至少同时包含有这两项的就查出

db.UserInfo.find({"acl": {"$in": ["admin", "root"]}})  // 包含有这两项的,都查出;

db.UserInfo.find({"acl": {$all: ["admin", "root"]}}) //1
db.UserInfo.find({acl: {$all: ["admin", "root"]}})  //1


db.UserInfo.find( { $and: [ { acl: [ "admin", "root" ] } ] } )  //mongodb-win32-x86_64-windows-5.0.12  试了,只同时包含这两项才出现

db.UserInfo.find({ $and: [ { acl: "admin" }, { acl: "root" } ] }) //3      1与3的效果一样

Equivalent to $and Operation

The $all is equivalent to an $and operation of the specified values; i.e. the following statement:

{ tags: { $all: [ "ssl" , "security" ] } }

is equivalent to:

{ $and: [ { tags: "ssl" }, { tags: "security" } ] }

$all — MongoDB Manualhttps://www.mongodb.com/docs/upcoming/reference/operator/query/all/#op._S_all

以下是ORM Bee通用页面对象的方法来操作MongoDB;
Bee也是有MongoDB ORM功能的。

用面向对象简单多了,不需要去记MongoDB的语法。

同时有,有找出,多了项的也不会匹配。

		List acl=new ArrayList<>();
		acl.add("admin");
		acl.add("root"); //两项的前后顺序无关
		UserInfo  userInfo2=new UserInfo();
		userInfo2.setAcl(acl); //同时有,有找出,多了项的也不会匹配。
		suid.select(userInfo2);

包含有其中一个项,即会找出

		List acl=new ArrayList<>();
		acl.add("admin");
		acl.add("root");
		UserInfo  userInfo2=new UserInfo();
		
		Condition condition=BF.getCondition();
		condition.op("acl", Op.in, acl);  //包含有其中一个项,即会找出

至少同时包含有这两项(可以多),与all效果一样.

		List acl=new ArrayList<>();
		acl.add("admin");
		List acl2=new ArrayList<>();
		acl2.add("root");
		UserInfo  userInfo2=new UserInfo();

//		db.UserInfo.find({"$and": [{"acl": {"$in": ["admin"]}}, {"acl": {"$in": ["root"]}}]})
		Condition condition=BF.getCondition();
		condition.op("acl", Op.in, acl);
//		condition.and();  //会自动加
		condition.op("acl", Op.in, acl2);
		
		suid.select(userInfo2,condition);

  • 8
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
To save the BSON in ASP.NET Core session, you can serialize the BSON to a byte array and store it in the session as a string. Here's an example: ``` using MongoDB.Bson; using MongoDB.Bson.IO; using MongoDB.Bson.Serialization; using MongoDB.Driver; using Microsoft.AspNetCore.Http; using System.IO; using System.Text; // Convert a list to BSON List<string> list = new List<string> { "one", "two", "three" }; BsonArray bsonArray = new BsonArray(list); BsonDocument bsonDocument = new BsonDocument { { "list", bsonArray } }; // Serialize the BSON to a byte array var memoryStream = new MemoryStream(); var writer = new BsonBinaryWriter(memoryStream); BsonSerializer.Serialize(writer, typeof(BsonDocument), bsonDocument); byte[] bsonBytes = memoryStream.ToArray(); // Convert the byte array to a string string bsonString = Encoding.UTF8.GetString(bsonBytes); // Save the BSON in session HttpContext.Session.SetString("bson", bsonString); ``` To retrieve the BSON from session and convert it back to a list: ``` // Retrieve the BSON from session string bsonString = HttpContext.Session.GetString("bson"); byte[] bsonBytes = Encoding.UTF8.GetBytes(bsonString); // Deserialize the BSON to a BsonDocument var memoryStream = new MemoryStream(bsonBytes); var reader = new BsonBinaryReader(memoryStream); BsonDocument bsonDocument = BsonSerializer.Deserialize<BsonDocument>(reader); // Convert the BsonDocument to a list BsonArray bsonArray = bsonDocument["list"].AsBsonArray; List<string> list = bsonArray.Select(x => x.AsString).ToList(); ``` Note that you need to add the `MongoDB.Driver` and `Microsoft.AspNetCore.Session` NuGet packages to your project to use the MongoDB C# driver and ASP.NET Core session.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值