Skip to content

Instantly share code, notes, and snippets.

@wongyouth
Created June 20, 2018 06:42
Show Gist options
  • Save wongyouth/08ec4fd315e414b973030d32e5a390f3 to your computer and use it in GitHub Desktop.
Save wongyouth/08ec4fd315e414b973030d32e5a390f3 to your computer and use it in GitHub Desktop.
Some memo to use mongodb

Mongodb

Mongo Console Shell

db.coll.find().forEach(doc => {})

loop all documents and update

db.coll.find().forEach(doc => { // ... change doc

// update db.coll.save(doc) })

nodejs code

db.coll.find().forEach(doc => {}, (err, result) => {})

删除列表中的键值

误操作增加了key键后,如何删除所有words 中的 key

db.lesson.find({'words.key': {$exists: true}}).forEach(doc => { ... doc.words.forEach(word => { ... delete word.key ... }) ... ... db.lesson.save(doc) ... })

$ 只匹配第一个找到的元素,所以以下这个做法达不到效果

db.lesson.updateMany({'words.key': ''}, {$unset: {'words.$.key': 1}})

匹配技巧

只打印匹配的元素

只打印 words 列表里 sound 含有单引号的文档,不显示不含单引号的word

db.lesson.find({"words.sound": {$regex: "'"}}, {words: {$elemMatch: {sound: {$regex: "'"}}}})

{ "_id" : ObjectId("5930c492a09e1dfceff2ebb0"), "words" : [ { "word" : "can't", "sound" : "/upload/sound/can't.mp3", "mp3_url" : "http://word.kingsunsoft.com/upload/sound/can't.mp3", "phonogram" : "/kænt/", "acceptation" : "

aux. ( =can not)不能

" } ] } { "_id" : ObjectId("5930c4eca09e1dfceff2ec0a"), "words" : [ { "word" : "don't", "sound" : "/upload/sound/don't.mp3", "mp3_url" : "http://word.kingsunsoft.com/upload/sound/don't.mp3", "phonogram" : "/dəʊnt/", "acceptation" : "aux. (=do not) 不是;不要;不做,不干" } ] } { "_id" : ObjectId("5930c504a09e1dfceff2ec22"), "words" : [ { "word" : "don't", "sound" : "/upload/sound/don't.mp3", "mp3_url" : "http://word.kingsunsoft.com/upload/sound/don't.mp3", "phonogram" : "/dəʊnt/", "acceptation" : "aux. (=do not) 不是;不要;不做,不干" } ] } { "_id" : ObjectId("5930c50fa09e1dfceff2ec2d"), "words" : [ { "word" : "doesn't", "sound" : "/upload/sound/doesn't.mp3", "mp3_url" : "http://word.kingsunsoft.com/upload/sound/doesn't.mp3", "phonogram" : "/ˈdʌznt/", "acceptation" : "aux./v. ( =does not)不做,不干" } ] } { "_id" : ObjectId("5930c57ba09e1dfceff2ec95"), "words" : [ { "word" : "don't", "sound" : "/upload/sound/don't.mp3", "mp3_url" : "http://word.kingsunsoft.com/upload/sound/don't.mp3", "phonogram" : "/dəʊnt/", "acceptation" : "aux. (=do not) 不是;不要;不做,不干" } ] }

https://stackoverflow.com/questions/3985214/retrieve-only-the-queried-element-in-an-object-array-in-mongodb-collection

列表元素数量大于1的所有文档

db.lesson.aggregate( [ ... { $project: {words: 1} }, ... { $match: {words: {$ne: null}} }, ... { $unwind : "$words" }, ... { $group : { _id : "$words.sound", words: {$addToSet: "$words.word"} } }, ... { $project: { words: 1, count: {$size: '$words' }}}, ... { $match: {count: {$gt: 1}}} ... ])

如果不在aggregate 中,还可以使用 $where

db.lesson.find({$where: 'this.words.length > 1'})

https://stackoverflow.com/questions/7811163/query-for-documents-where-array-size-is-greater-than-1

  • 搜索未设置字段

      db.lesson.find({words: {$exists: false}})
    
  • 搜索有设置字段

      db.lesson.find({words: {$exists: true}})
    
  • 搜索字段为null

      db.lesson.find({words: null}).count()
    
  • 搜索字段不为null

      db.lesson.find({words: {$ne: false}})
    
  • 搜索字段数组长度为0

      db.lesson.find({words: {$size: 0}})
    
  • 搜索字段数组长度大于0

      db.lesson.find({$where: 'this.words.length > 0'})
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment