[CSHARP-812] Nested object list and query nested columns,but doesn't match any value Created: 02/Sep/13  Updated: 05/Apr/19  Resolved: 20/Jun/14

Status: Closed
Project: C# Driver
Component/s: Setup
Affects Version/s: 1.8.2
Fix Version/s: None

Type: Task Priority: Major - P3
Reporter: bivozeou Assignee: Unassigned
Resolution: Done Votes: 0
Labels: driver, query, question
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

microsoft winodws , visual studio 2010 ,mongodb 2.4.3


Attachments: File Test.rar    

 Description   

detail is here

table Imb_MicroBlog

public class Imb_MicroBlog{
   public Guid _id { get; set; }
   public Guid createUserID { get; set; }
   public List<Imb_Comment> commentList { get; set; }
   public bool isAudit{ get; set ; }
   public string content{ get; set ; }
}

table Imb_Comment

public class Imb_Comment{
   public Guid _id { get; set; }
   public Guid createUserID { get; set; }
   public bool isAudit{ get; set ; }
   public string content{ get; set ; }
}

when i want to match nested Imb_Comment's cloumns isAudit=true and Slice them
C# like this

collection.Find(
    Query.And(
        Query.EQ("_id", microblogID), 
        Query.EQ("commentList.isAudit", True)
    )
)
.SetFields(Fields.Slice("commentList", skip, limit))
.ToList()
[0].commentList[0].replyList.Count;

but it match all rows include isAudit=false, i don't how to query nested item ,hope help me thanks very much



 Comments   
Comment by Robert Stam [ 20/Mar/14 ]

Sorry for the delay in responding... are you still interested in pursuing this? If so, I'm still not exactly clear on what the question is. Can you provide some sample documents that you want to match the query and some that you don't want to match the query? Perhaps describe in words what you want the query to do and we can try to come up with a query that does that.

Thanks!

Comment by bivozeou [ 03/Sep/13 ]

Mongo Action 1: in this way , if one row matched all row matched C#:

collection.Find(Query.And(Query.EQ("_id", microblogID), Query.EQ("commentList.isAudit", true)))

in mongo shell cmd :

db.Imb_Comment.find({ "_id" : "709fb1e5-6e2f-49d4-ab3d-103f5f95453e", "commentList.isAudit" : true })

Mongo Action 2: in this way , just can matched no more than one row C#:

collection.Find(Query.EQ("_id", microblogID)).SetFields(Fields.ElemMatch("commentList", Query.EQ("isAudit", true)))
 

in mongo shell cmd :

db.Imb_Comment.find({ "_id" : "709fb1e5-6e2f-49d4-ab3d-103f5f95453e" },{ "commentList" : { "$elemMatch" : { "isAudit" : true } } })

please change the mongohelper connectionString to your mangodb address

Comment by Robert Stam [ 02/Sep/13 ]

Have you tried to get this working the MongoDB shell? Sometimes it's useful to experiment in the shell and then translate to C#.

One thing to consider is that when you query against an array element (i.e.

{ "commentList.isAudit" : true }

) it will match a document where any of the entries in the commentList have isAudit set to true.

Can you provide a sample document that you think should not be matched?

Generated at Wed Feb 07 21:37:54 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.