mongodb-query

Use of collation in mongodb $regex

邮差的信 提交于 2019-12-04 10:48:47
Since v3.4 collations are available for find operations, especially as matches for diacritic characters are concerned. While a find query with a definite value ($eq opeartor or corresponding construct) will match letters and correspondent diacritics, the same is not true if a $regex is used in order to achieve a match on a partial search string (a 'LIKE'). Is there a to make the $regex query use the collation the same way than the $eq query? consider example collection testcoll: { "_id" : ObjectId("586b7a0163aff45945462bea"), "city" : "Antwerpen" }, { "_id" : ObjectId("586b7a0663aff45945462beb

$lookup nested array in mongodb

无人久伴 提交于 2019-12-04 09:56:09
I am struggling with the newish (lovely) lookup operator in MongoDB. I have 3 collections: artists { "_id" : ObjectId("5b0d2b2c7ac4792df69a9942"), "name" : "Dream Theater", "started_in" : NumberInt(1985), "active" : true, "country" : "US", "current_members" : [ ObjectId("5b0d2a7c7ac4792df69a9941") ], "previous_members" : [ ObjectId("5b0d2bf57ac4792df69a9954") ], "albums" : [ ObjectId("5b0d16ee7ac4792df69a9924"), ObjectId("5b0d47667ac4792df69a9994") ], "genres" : [ "prog metal", "prog rock" ] } Albums { "_id" : ObjectId("5b0d16ee7ac4792df69a9924"), "title" : "Images and words", "released" :

MongoDB shell: how to search for collections which match a name or regex

旧时模样 提交于 2019-12-04 08:57:35
问题 when I use show collections it returns a list of all collections which is pretty long, how can I write a query to return collections matching a pattern. I was hoping for something like db.collections({name:/pattern/}) but couldn't find 回答1: You can use db.getCollectionNames() with Array.filter(): db.getCollectionNames().filter(function (collection) { return /pattern/.test(collection) }) 来源: https://stackoverflow.com/questions/26684307/mongodb-shell-how-to-search-for-collections-which-match-a

mongodb aggregate embedded document values

喜欢而已 提交于 2019-12-04 08:35:38
问题 Im Strugling with some aggregation functions in mongodb. Say I have some documents like this [ { _id: "1", periods: [ { _id: "12", tables: [ { _id: "121", rows: [ { _id: "1211", text: "some text"}, { _id: "1212", text: "some other text"}, { _id: "1213", text: "yet another text"}, ] } ] }, { _id: "13", tables: [ { _id: "131", rows: [ { _id: "1311", text: "different text"}, { _id: "1312", text: "Oh yeah"} ] } ] } ] }, { _id: "2", periods: [ { _id: "21", tables: [ { _id: "212", rows: [ { _id:

Mongoose findOneAndUpdate Updating Multiple Fields

China☆狼群 提交于 2019-12-04 08:25:20
问题 The findOneAndUpdate method doesn't work properly. I'm trying to update all the fields all at once but it's only updating (setting) the last field. It's always only the last field. Can someone tell me what I'm doing wrong or what can I do to have the expected effect? This is my findOneAndUpdate code: Book.findOneAndUpdate({_id:bookId},{$set:{"name": name},$set:{"genre": genre},$set:{"author": author},$set:{"similar": similar}}).exec(function(err, book){ if(err){ console.log(err); res.status

Most common distinct values mongodb

一笑奈何 提交于 2019-12-04 08:24:33
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 is count 2 How to query it in NodeJs mongo client? 2017-08-01 Update As release of MongoDB 3.4, the

how to convert timestamp to date in mongodb?

為{幸葍}努か 提交于 2019-12-04 08:09:49
i am working on aggregation of mongodb collection.my mongodb collection has creation_time in timestamp.How will i can collect same day result and aggregate with next day result.Suppose i have to collect data for 5 days.My mongodb collection is: { "_id" : "1522845653126" }, { "_id" : "1522838153324" }, { "_id" : "1513421466415" }, { "_id" : "1515488183153" }, { "_id" : "1521571234500" } How can i calculate.How many entry are save on specific date?? nd suppose I want is to query results that returns a running total of the aggregation, like: { time: "2013-10-10" count: 3, }, { time: "2013-10-11"

Closing MongoDB Java Connection

梦想与她 提交于 2019-12-04 07:20:57
I am trying to design a Mongo Db connection class where I am maintaning MongoClient as static. private static MongoClient client = null; public static DB connectToMongo() throws Exception { if (null != client) { return client.getDB(DBNAME); } client = new MongoClient(HOST,PORT); return client.getDB(DBNAME); } My whole web application uses the above method to connect to Mongo as follows: db = MongoDBConnection.connectToMongo(); collection = db.getCollection("collectionName"); After performing DB operations I never call the close connection for MongoClient. The connection class would always

MongoDB Aggregation Framework - Dynamic Field Rename

怎甘沉沦 提交于 2019-12-04 06:55:17
I am finding the mongodb aggregation framework to be extremely powerful - it seems like a good option to flatten out an object. My schema uses a an array of sub objects in an array called materials. The number of materials is variable, but a specific field (category) will be unique across objects in the array. I would like to use the aggregation framework to flatten the structure and dynamically rename the fields based on the value of the category field. I could not find an easy way to accomplish this using a $project along with $cond. Is there a way? The reason for the array of material

MongoDB update multiple subdocuments with or query

半腔热情 提交于 2019-12-04 06:46:33
问题 I want to make an update query for multiple subdocuments at once. I will be setting all the subdocuments statuses to passive where they satisfy the condition I give on query. db.deduplications.update( { $and: [ { "_id": "189546D623FC69E3B693FDB679DBC76C" }, { "DeviceVersionPairs.DeviceId": ObjectId("5822d0606bfdcd6ec407d9b9") }, { "DeviceVersionPairs.CloudFolderId": ObjectId("5823110e6bfdd46ec4357582") }, { "DeviceVersionPairs.CloudFileId": ObjectId("582311168cd396223499942a") }, {