我在MongoDB中有以下集合:
using iTextSharp.text;
using iTextSharp.text.pdf;
Mongo Shell中的查询是什么//Create a byte array that will eventually hold our final PDF
Byte[] bytes;
//Boilerplate iTextSharp setup here
//Create a stream that we can write to, in this case a MemoryStream
using (var ms = new MemoryStream())
{
//Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
using (var doc = new Document())
{
//Create a writer that's bound to our PDF abstraction and our stream
using (var writer = PdfWriter.GetInstance(doc, ms))
{
//Open the document for writing
doc.Open();
string finalHtml = string.Empty;
// read your html by database or from a html file here and store it into finalHtml e.g. a string
//XMLWorker also reads from a TextReader and not directly from a string
using (var srHtml = new StringReader(finalHtml))
{
//Parse the HTML
iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
}
doc.Close();
}
}
//After all of the PDF "stuff" above is done and closed but **before** we
//close the MemoryStream, grab all of the active bytes from the stream
bytes = ms.ToArray();
}
//clear the response
Response.Clear();
MemoryStream mstream = new MemoryStream(bytes);
//define response content type
Response.ContentType = "application/pdf";
//give the name of file of pdf and add in to header
Response.AddHeader("content-disposition", "attachment;filename=invoice.pdf");
Response.Buffer = true;
mstream.WriteTo(Response.OutputStream);
Response.End();
有{
"id": 123,
"profile":{
"name": "name",
"age":45,
"wishlist": [
{"_id":1, "name":"a1"},
{"_id":2, "name":"a2"},
{"_id":3, "name":"a3"}
]
}
}
的集合?
答案 0 :(得分:5)
当您与一个字段匹配时,您只需使用点符号表示您的字段的路径:
> db.user.find({"profile.wishlist._id": 2})
如MongoDB documentation中所述,对于数组(如wishlist
),如果数组中的任何子文档与字段值匹配,则这将匹配文档。
请注意,如果您需要与几个字段匹配,则需要使用以下任一字段:
$elemMatch
如果所有匹配的字段都属于相同的子文档; 请比较这两个查询的输出以掌握这一点:
> db.user.find({"profile.wishlist._id": 2, "profile.wishlist.name": "a1"})
// ^ ^^
// will return your document even if the was no
// subdocument having both _id=2 and name=a1
> db.user.find({"profile.wishlist": {$elemMatch: { _id: 2, name: "a1"}}})
// ^ ^^
// no result as there was no subdocument
// matching _both_ _id=2 and name=a1