最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】mongo的shell中find返回多个有限个数的结果

MongoDB crifan 2129浏览 0评论

折腾:

【已解决】把本地mp3文件存入在线Mongo中且填写meta信息

期间:

<code>&gt; db.fs.files.findOne()
{
        "_id" : ObjectId("5b20d5ec7f4d384006e91f18"),
        "contentType" : "audio/mpeg",
        "chunkSize" : 261120,
        "metadata" : {
                "song" : {
                        "singers" : [ ]
                },
                "fitAgeStart" : 3,
                "topics" : [
                        "Wisdom",
                        "Animals",
                        "Villain"
                ],
                "storybook" : {
                        "publisher" : "Mathew Price",
                        "isFiction" : "是",
                        "lexileIndex" : "",
                        "awards" : "",
                        "authors" : [
                                "Atsuko Morozumi"
                        ],
                        "foreignCountry" : "英国"
                },
                "keywords" : {
                        "fromName" : [
                                "go away Mr Wolf",
                                "Mr wolf",
                                "Go away",
                                "wolf"
                        ],
                        "other" : [ ],
                        "fromContent" : [
                                "knock!",
                                "go away",
                                "Mr Wolf",
                                "quickly shut the door"
                        ]
                },
                "name" : "Go away, Mr Wolf",
                "resourceType" : "storybook",
                "mainActors" : [
                        "three little pigs",
                        "Mr.Wolf"
                ],
                "contentAbstract" : "一个教小朋友警惕坏人的故事,狼先生扮成各种样子前来敲小猪家的们,3只小猪识别了狼的真面目,把门关上不让狼进来,一直到爸爸回来把发生的事情告诉爸爸听。",
                "isSeries" : false,
                "series" : {
                        "number" : 0,
                        "name" : ""
                },
                "fitAgeEnd" : 4,
                "fileInfo" : {
                        "isAudio" : true,
                        "contentType" : "audio/mpeg",
                        "name" : "Go away, Mr Wolf.mp3",
                        "suffix" : "mp3"
                }
        },
        "filename" : "Go away, Mr Wolf.mp3",
        "length" : 1613790,
        "uploadDate" : ISODate("2018-06-13T08:29:32.493Z"),
        "md5" : "3fa28e2ef5e7e56e15f797cd169845cc"
}
</code>

但是每次

db.fs.files.findOne()

只能输出第一个

想要多找几个gridfs的file看看信息

想要稍微多输出几个

mongo gridfs find several

mongo find several

Limit the Number of Documents to Return

是我要的

<code>&gt; db.fs.files.find().limit(3)
{ "_id" : ObjectId("5b20d5ec7f4d384006e91f18"), "contentType" : "audio/mpeg", "chunkSize" : 261120, "metadata" : { "song" : { "singers" : [ ] }, "fitAgeStart" : 3, "topics" : [ "Wisdom", "Animals", "Villain" ], "storybook" : { "publisher" : "Mathew Price", "isFiction" : "是", "lexileIndex" : "", "awards" : "", "authors" : [ "Atsuko Morozumi" ], "foreignCountry" : "英国" }, "keywords" : { "fromName" : [ "go away Mr Wolf", "Mr wolf", "Go away", "wolf" ], "other" : [ ], "fromContent" : [ "knock!", "go away", "Mr Wolf", "quickly shut the door" ] }, "name" : "Goaway, Mr Wolf", "resourceType" : "storybook", "mainActors" : [ "three little pigs", "Mr.Wolf" ], "contentAbstract" : "一个教小朋友警惕坏人的故事,狼先生扮成各种样子前来敲小猪家的们,3只小猪识别了狼的真面目,把门关上不让狼进来,一直到爸爸回来把发生的事情告诉爸爸听。", "isSeries" :false, "series" : { "number" : 0, "name" : "" }, "fitAgeEnd" : 4, "fileInfo" : { "isAudio" : true, "contentType" : "audio/mpeg", "name" : "Go away, Mr Wolf.mp3", "suffix" : "mp3" } }, "filename" : "Go away, Mr Wolf.mp3", "length" : 1613790, "uploadDate" : ISODate("2018-06-13T08:29:32.493Z"), "md5" : "3fa28e2ef5e7e56e15f797cd169845cc" }
{ "_id" : ObjectId("5b20d5ec7f4d384006e91f20"), "contentType" : "audio/mpeg", "chunkSize" : 261120, "metadata" : { "song" : { "singers" : [ ] }, "fitAgeStart" : 0, "topics" : [ ], "storybook" : { "publisher" : "", "isFiction" : "未知", "lexileIndex" : "", "awards" : "", "authors" : [ "Robert Munsch" ], "foreignCountry" : "" }, "keywords" : { "fromName" : [ "Love you", "Love", "forever" ], "other" : [ ], "fromContent" :[ ] }, "name" : "Love You Forever", "resourceType" : "storybook", "mainActors" : [ ], "contentAbstract" : "", "isSeries" : false, "series" :{ "number" : 0, "name" : "" }, "fitAgeEnd" : 0, "fileInfo" : { "isAudio" : true, "contentType" : "audio/mpeg", "name" : "Love You Forever.mp3", "suffix" : "mp3" } }, "filename" : "Love You Forever.mp3", "length" : 2324504, "uploadDate" : ISODate("2018-06-13T08:29:32.611Z"), "md5" : "12bd627611ad4b0f8806c7182414c155" }
{ "_id" : ObjectId("5b20d5ec7f4d384006e91f2a"), "contentType" : "audio/mpeg", "chunkSize" : 261120, "metadata" : { "song" : { "singers" : [ ] }, "fitAgeStart" : 0, "topics" : [ ], "storybook" : { "publisher" : "", "isFiction" : "未知", "lexileIndex" : "", "awards" : "", "authors" : [ "Anthony Brown" ], "foreignCountry" : "" }, "keywords" : { "fromName" : [ "I like", "book" ], "other" : [ ], "fromContent" : [ ] }, "name" : "I like books", "resourceType" : "storybook", "mainActors" : [ ], "contentAbstract" : "", "isSeries" : false, "series" : { "number" : 0, "name" : "" }, "fitAgeEnd" : 0, "fileInfo" : { "isAudio" : true, "contentType" : "audio/mpeg", "name" : "I like books.mp3", "suffix" : "mp3" } }, "filename" : "I like books.mp3", "length" : 1679723, "uploadDate" : ISODate("2018-06-13T08:29:32.681Z"), "md5" : "5615c4b2f711a76c400b87f0586e3c4d" }
&gt;
</code>

然后再去用:

> db.fs.files.find().limit(10)

找到一个其他类型的,供参考:

<code>{ "_id" : ObjectId("5b20d5ed7f4d384006e91f96"), "contentType" : "application/pdf", "chunkSize" : 261120, "metadata" : { "song" : { "singers": [ ] }, "fitAgeStart" : 5, "topics" : [ "Love", "Animals" ], "storybook" : { "publisher" : "", "isFiction" : "是", "lexileIndex" : "", "awards" : "", "authors" : [ "Jez Alborough" ], "foreignCountry" : "" }, "keywords" : { "fromName" : [ "my Teddy", "where is Teddy", "where Teddy", "Teddy", "where" ], "other" : [ ], "fromContent" : [ ] }, "name" : "Where is my Teddy", "resourceType" : "storybook", "mainActors" : [ "Eddie", "bear" ], "contentAbstract" : "Eddie和一只熊都在找属于自己的Teddy,Eddie找到了属于熊的,太大了。熊找到了属于Eddie的,太小了,最后他们都拿回了属于自己的Teddy,满意地抱着钻进被窝。", "isSeries" : false, "series" : { "number" : 0, "name" : "" }, "fitAgeEnd" : 6, "fileInfo" : { "isAudio" : false, "contentType" : "application/pdf", "name" : "Where is my Teddy.pdf", "suffix" : "pdf" } }, "filename" : "Where is my Teddy.pdf", "length" : 16268867, "uploadDate" : ISODate("2018-06-13T08:29:34.072Z"), "md5" : "b32f1b0c8b02a8a5e7dbb04ecaaac4cd" }
</code>

【总结】

通过:

<code>db.fs.files.find().limit(10)
</code>

可以输出指定个数(此处10)个结果

延伸/举一反三:

可以通过:

db.collection.find() — MongoDB Manual

知道还有其他命令:

<code>db.bios.find().limit( 5 )
db.bios.find().skip( 5 )
db.bios.find().sort( { name: 1 } )
</code>

更多命令,参考官网文档。

转载请注明:在路上 » 【已解决】mongo的shell中find返回多个有限个数的结果

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.171 seconds, using 22.12MB memory