mongodb-query

Most common distinct values mongodb

穿精又带淫゛_ 提交于 2019-12-09 18:21:38
问题 How to get the most common distinct values of a key in query results. Consider a collection 'collectionSample' { name : 'a', value: 10, word : 'baz' }, { name : 'a', value: 65, word : 'bar' }, { name : 'a', value: 3, word : 'foo' }, { name : 'b', value: 110, word : 'bar' }, { name : 'b', value: 256, word : 'baz' } Here I want to find the mode of key 'name', that is the most repeated distinct 'name'. The result I'm hoping to get is like {'most_common_distinct_val':a} //since a is count 3 and b

How to read a specific key-value pair from mongodb collection

非 Y 不嫁゛ 提交于 2019-12-09 18:10:26
问题 If I have a mongodb collection users like this: { "_id": 1, "name": { "first" : "John", "last" :"Backus" }, } How do I retrieve name.first from this without providing _id or any other reference. Also, is it possible that pulling just the `name^ can give me the array of embedded keys (first and last in this case)? How can that be done? db.users.find({"name.first"}) didn't work for me, I got a: SyntaxError "missing: after property id (shell):1 回答1: The first argument to find() is the query

Match at least “N” elements of an array to a list of conditions

对着背影说爱祢 提交于 2019-12-09 13:32:04
问题 I have the following scenario: One of my mongo collections has documents in the following format: user: "test", tracks: [{artist: "A", ...}, {artist: "B", ...}, ..., { artist: "N", ...}] I would like to extract all the tracks, whose artists are in a given array arr . For that purpose I'm using the following query (which works fine). collection.find({ tracks: { $elemMatch: { artist: { $in: arr }}}}) However, now I would like to modify the query so that it returns only those documents in the

Mongodb aggregate three collections

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 12:31:29
问题 Learning MongoDB for the past two days and I am trying to aggregate three collections but unable to achieve it Below are the four collection maintaining in the database university { "_id" : "5834ecf7432d92675bde9d82", "name": "NIFT" } college { "_id" : "5834ecf7432d92675bde9d83", "name": "NIFT Hyderabad", "university_id":"5834ecf7432d92675bde9d82" } departments { "_id" : "5834ecf7432d92675bde9d84", "department_name": "Fashion Technology", "college_id" : "5834ecf7432d92675bde9d83" }, { "_id" :

How to return number of updated objects in mongodb?

北战南征 提交于 2019-12-09 11:20:51
问题 I'm updating multiple elements in mongodb. Is it possible to return the number of affected objects? 回答1: Use getLastError. The n key will contain the number of updated documents > db.count.update({x : 1}, {$inc : {x : 1}}, false, true) > db.runCommand({getLastError : 1}) { "err" : null, "updatedExisting" : true, "n" : 5, "ok" : true } Note that this runs the command "getLastError" which returns the number of rows after the update command has completed. Database commands are listed here. 来源:

How to sort documents based on length of an Array field

耗尽温柔 提交于 2019-12-09 11:20:20
问题 In my small ExpressJS app, I have a Question model which was defined like that var mongoose = require('mongoose'), Schema = mongoose.Schema; /** * Question Schema */ var Question = new Schema({ title: { type: String, default: '', trim: true, required: 'Title cannot be blank' }, content: { type: String, default: '', trim: true }, created: { type: Date, default: Date.now }, updated: { type: Date, default: Date.now }, author: { type: Schema.ObjectId, ref: 'User', require: true }, answers : [{

Aggregate Query in Mongodb returns specific field

我的未来我决定 提交于 2019-12-09 10:45:09
问题 Document Sample: { "_id" : ObjectId("53329dfgg43771e49538b4567"), "u" : { "_id" : ObjectId("532a435gs4c771edb168c1bd7"), "n" : "Salman khan", "e" : "salman@gmail.com" }, "ps" : 0, "os" : 1, "rs" : 0, "cd" : 1395685800, "ud" : 0 } Query: db.collectiontmp.aggregate([ {$match: {os:1}}, {$project : { name:{$toUpper:"$u.e"} , _id:0 } }, {$group: { _id: "$u._id",total: {$sum:1} }}, {$sort: {total: -1}}, { $limit: 10 } ]); I need following things from the above query: Group by u._id Returns total

MongoDB on mLab authentication fails

让人想犯罪 __ 提交于 2019-12-09 10:38:49
问题 Created a new mLab account and created a database as per the steps here http://docs.mlab.com/#create-sub . Trying to connect to the database using mongo shell and mongoose Node.js module but I see the 'Authentication Failed' errorin both cases. In Mongo shell the command is, I have double checked the credentials mongo ds012345.mlab.com:56789/dbname -u dbuser -p dbpassword Error: MongoError: authentication fail at Function.MongoError.create (E:\Gatsby\notmongoose\node_modules\mongodb-core\lib

How to use $in or $nin in mongo aggregation $group $cond

旧巷老猫 提交于 2019-12-09 09:56:55
问题 I want to achieve $sum with $cond by having $or on property: db.collectionName.aggregate( { "$group": { "_id":'$created_at', "count": {"$sum": 1}, "count_failure": { "$sum": { "$cond": [ { "$id": { "$in": [ 0,100,101,102,103,104,105 ] } }, 1, 0 ] } } } } ) But error says: Invalid operator "$id" What's wrong with syntax? Or I am writing query wrongly. Currently I am achieving this by: db.collectionName.aggregate( { "$group": { "_id":'$created_at', "count": {"$sum": 1}, "count_failure": { "$sum

Mongo DB query in java

假如想象 提交于 2019-12-09 08:39:21
问题 I have to write a complex mongo query using java but am not able to do it. The mongo query looks like this: db.video.findOne( { $or: [ { key1: { $in : [764] } }, { key2: {$in : [list2] } }, { $and [ { key2 : 3}, {key4:67} ] } ] }) I have to write the above query using the QueryBuilder class. In what way can I do it? Thanks 回答1: Using QueryBuilder your query should look like this DBObject query = QueryBuilder.start().or( QueryBuilder.start("key1").in(764).get(), QueryBuilder.start("key2").in