mongodb-query

mongoDB : updating an objects using dot notation (multi-level object)

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:11:31
问题 I have an object with the following form : { "_id": ObjectId("4fa43f4d1cf26a6a8952adf1"), "userId": "1", "facebookId": "1234", "groups": [{ "groupName": "testGroup", "members": [{ "memberFirstName": "userFirstName", "memberLastName": "userLastName", "memberDetails": { "userId": "1", "faceBookId": "1234" } }] }] } It's a collection that holds for each user - its groups, with each group containing the group members... So the "groups" key is an array (or list), and so is the "members" key. (each

How to efficiently page batches of results with MongoDB

廉价感情. 提交于 2019-12-12 10:03:49
问题 I am using the below query on my MongoDB collection which is taking more than an hour to complete. db.collection.find({language:"hi"}).sort({_id:-1}).skip(5000).limit(1) I am trying to to get the results in a batch of 5000 to process in either ascending or descending order for documents with "hi" as a value in language field. So i am using this query in which i am skipping the processed documents every time by incrementing the "skip" value. The document count in this collection is just above

How to Count Matched array elements

╄→尐↘猪︶ㄣ 提交于 2019-12-12 09:58:59
问题 I have a collection and each document in that collection has an array field countries . I want to select all documents which include any of below countries: China, USA, Australia And the output should show the number of above countries each document has. I use below aggregate command: db.movies.aggregate([ { $match: { countries: { $in: ["USA", 'China', 'Australia'] } } }, { $project: { countries: {$size: '$countries'} } } ]); it doesn't work as expected. It shows the number of all countries

How to use Aggregate in mongoose

蹲街弑〆低调 提交于 2019-12-12 08:37:16
问题 How do I define the following MongoDB aggregate query in mongoose: db.contacts.aggregate([{$group: { "_id": { code: "$Code", name: "$Name" } } }]) The objective of the query is to pull a list of distinct codes and names. My current model code is: 'use strict'; var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = Schema.ObjectId; var fields = { Code: { type: String }, Name: { type: String } }; var contactSchema = new Schema(fields); module.exports = mongoose.model('Contacts

How to get the maximum value of a field for each group with the array of corresponding documents?

佐手、 提交于 2019-12-12 08:35:04
问题 I have a collection like { "_id" : ObjectId("5738cb363bb56eb8f76c2ba8"), "records" : [ { "Name" : "Joe", "Salary" : 70000, "Department" : "IT" } ] }, { "_id" : ObjectId("5738cb363bb56eb8f76c2ba9"), "records" : [ { "Name" : "Henry", "Salary" : 80000, "Department" : "Sales" }, { "Name" : "Jake", "Salary" : 40000, "Department" : "Sales" } ] }, { "_id" : ObjectId("5738cb363bb56eb8f76c2baa"), "records" : [ { "Name" : "Sam", "Salary" : 90000, "Department" : "IT" }, { "Name" : "Tom", "Salary" :

How to find length of the array mongodb

非 Y 不嫁゛ 提交于 2019-12-12 07:22:52
问题 I have such this type of structure of my database. I want to find number of documents my "JAN" array contains. I am trying to do in different way but nothing works. That would be great if someone helps. { "_id" : ObjectId("5cd8609db20663a19e2c362c"), "2017" : { "JAN" : [ { "SYMBOL" : "20MICRONS", "SERIES" : "EQ", "OPEN" : "33.4", "HIGH" : "34.3", "LOW" : "33", }, { "SYMBOL" : "3IINFOTECH", "SERIES" : "EQ", "OPEN" : "5.8", "HIGH" : "5.8", "LOW" : "5.55", }, { "SYMBOL" : "3MINDIA", "SERIES" :

Is there a way to project the type of a field

删除回忆录丶 提交于 2019-12-12 06:46:47
问题 Suppose we had something like the following document, but we wanted to return only the fields that had numeric information: { "_id" : ObjectId("52fac254f40ff600c10e56d4"), "name" : "Mikey", "list" : [ 1, 2, 3, 4, 5 ], "people" : [ "Fred", "Barney", "Wilma", "Betty" ], "status" : false, "created" : ISODate("2014-02-12T00:37:40.534Z"), "views" : 5 } Now I know that we can query for fields that match a certain type by use of the $type operator. But I'm yet to stumble upon a way to $project this

Get And Sort In Array Mongodb

耗尽温柔 提交于 2019-12-12 06:07:08
问题 I have document like this { "id": "1", "myarray": [ { "_id": "4", "name": "d", "order": 4 }, { "_id": "1", "name": "a", "order": 1 }, { "_id": "2", "name": "b", "order": 2 } ] } i want to see sort data in array by order when get from db i am trying some query like below but it seems not OK and nothing changed db.collection.aggregate([{$unwind: "$myarray"},{$project: {orders:"$myarray.order"}},{$group: {_id:"$_id",min:{$min: "$orders"}}},{$sort: {min:1}}]) db.myarray.find({},{$sort:{"myarray

deep populating in mongoose

痴心易碎 提交于 2019-12-12 05:58:28
问题 I've got two schemas, one for user and another one for post in the user schema, I've got a property for latestPost which would be an ObjectId of an entry in the post schema when I load up the user object, I want to get the lastestPost as an object that includes the author's username from the user schema where the author is an ObjectId that'd match an _id field in the user schema. the mongoose tutorials seem to use the syntax of User.findOne({ _id: req.user.id}) .populate('latestPost')

MongoDB where clause

你。 提交于 2019-12-12 05:38:03
问题 I have mongoDB "users" collection in JSON format and I want to return all the data having privacy is true. How can I do it ? { "name" : "Maria Kari", "social" : [ { "facebook" : "www.fb.com/maria.mongodb", "privacy" : true }, { "twitter" : "www.tw.com/mongodb", "privacy" : false } ], "personal" : [ { "cell_no" : "+1-99082198414", "privacy" : true }, { "email" : "maria@mongodb.com", "privacy" : false } ] } Here, I want to return the data having privacy is true. For example, facebook, it has