• MongoDB简单使用-查询操作3


    首先添加几个文档:

    dvd = ({"Type":"DVD","Title":"Matrix,The","Released":1994,"Cast":["Keanu Reeves","Carrie-Anne Moss","Laurence Fishburne","Hugo Weaving","Gloria Foster","Joe Pantoliano"]})

    db.media.insertOne(dvd)

    dvd = ({"Type":"DVD","Title":"Blade Runner","Released":1982})
    db.media.insertOne(dvd)

    dvd = ({"Type":"DVD","Title":"Toy Story 3","Released":2010})

    db.media.insertOne(dvd)

    1.比较符号($gt,$gte,$lt,$lte)  #(大于,大于等于,小于,小于等于)

    db.media.find({Released:{$gt:2000}},{"Cast":0})

    系统返回
    { "_id" : ObjectId("5aa4984153350e04ddbd6fab"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }

    db.media.find({Released:{$gte:1994,$lt:2010}},{"Cast":0})

    系统返回
    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994 }

    2.$in,$all,$or

    db.media.find({Released:{$in:[1994,2008,2009]}},{"Cast":0}

    系统返回

    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994 }


    db.media.find({Released:{$all:[1994,2008,2009]}},{"Cast":0})

    系统返回

    (又不存在同时满足3个条件的文档,返回空)

    db.media.find({$or:[{"Title":"Toy Story 3"},{"ISBN":"978-1-4842-1183-0"}]})

    系统返回
    { "_id" : ObjectId("5aa490f053350e04ddbd6fa6"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB 3rd ed.", "ISBN" : "978-1-4842-1183-0", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }

    3.$slice

    db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:3}})

    系统返回
    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne" ] }

    db.media.find({"Title":"Matrix,The"},{"Cast":{$slice:[-5,4]}})   #从最后5项中截取前4个

    系统返回
    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster" ] }

    4.$mod

    db.media.find({Released:{$mod:[3,2]}},{"Cast":0})

    系统返回:

    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994 }
    { "_id" : ObjectId("5aa4981653350e04ddbd6faa"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }

    5.$size

    db.media.find({Tracklist:{$size:2}})

    系统返回:
    { "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

    6.$exists

    db.media.find({Author:{$exists:true}})

    系统返回:
    { "_id" : ObjectId("5aa490f053350e04ddbd6fa6"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB 3rd ed.", "ISBN" : "978-1-4842-1183-0", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }
    { "_id" : ObjectId("5aa4915453350e04ddbd6fa8"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB 3rd ed.", "ISBN" : "978-1-4842-1183-1", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }


    db.media.find({Author:{$exists:false}})

    系统返回:
    { "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }
    { "_id" : ObjectId("5aa4981653350e04ddbd6faa"), "Type" : "DVD", "Title" : "Blade Runner", "Released" : 1982 }
    { "_id" : ObjectId("5aa4984153350e04ddbd6fab"), "Type" : "DVD", "Title" : "Toy Story 3", "Released" : 2010 }

    7.$type

    db.media.find({Tracklist:{$type:3}})   #type:3表示嵌入式对象
    { "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

    8.$elemMatch

    db.media.find({"Tracklist.Title":"Smells Like Teen Spirit","Tracklist.Track":"1"})

    系统返回:
    { "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }
    { "_id" : ObjectId("5aa49fe653350e04ddbd6fac"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nirvana", "Tracklist" : [ { "Track" : "1", "Title" : "You Know You Are Right", "Length" : "3:38" }, { "Track" : "5", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" } ] }

    db.media.find({Tracklist:{$elemMatch:{Title:"Smells Like Teen Spirit",Track:"1"}}})

    系统返回:
    { "_id" : ObjectId("5aa4912953350e04ddbd6fa7"), "Type" : "CD", "Artist" : "Nirvana", "Title" : "Nevermind", "Tracklist" : [ { "Track" : "1", "Title" : "Smells Like Teen Spirit", "Length" : "5:02" }, { "Track" : "2", "Title" : "In Bloom", "Length" : "4:15" } ] }

    9.正则表达式

    db.media.find({Title:/^Matrix*/i})  #i表示不区分大小写

    系统返回:
    { "_id" : ObjectId("5aa497c753350e04ddbd6fa9"), "Type" : "DVD", "Title" : "Matrix,The", "Released" : 1994, "Cast" : [ "Keanu Reeves", "Carrie-Anne Moss", "Laurence Fishburne", "Hugo Weaving", "Gloria Foster", "Joe Pantoliano" ] }

  • 相关阅读:
    hdu5412CRB and Queries
    LCA rmq st model
    HDU 5348 MZL's endless loop
    2015多校联合训练赛 Training Contest 4 1008
    Bestcoder Tom and matrix
    TOJ 4105
    Codeforces D. Iahub and Xors
    Set 技巧之一
    1036: [ZJOI2008]树的统计Count
    一点点VIM
  • 原文地址:https://www.cnblogs.com/chzhang1994/p/8543735.html
Copyright © 2020-2023  润新知