mongodb-query

specifying $push location in collection using Pymongo

被刻印的时光 ゝ 提交于 2019-12-12 04:16:53
问题 I'm wondering how to use $push in a collection where I have to specify which document I want to add information to. Please consider the following example: student1 = { 'name': 'Adam', 'year': 'sophomore', 'age': 20, 'class':[ { 'className': 'cse131', 'time': '2:30', 'finalGrade': 'A' }, { 'className': 'cse240', 'time': '9:30', 'finalGrade': 'B' } ] } If I wanted to update only the first document in the 'class' collection (the cse131 in 'class'), how would I use $push to do this? For example,

want mongo query to do string regex start && contains && end

不羁的心 提交于 2019-12-12 04:14:28
问题 I want to mongo query with regex which will return the document name match start with 'abc' and name match contains 'efg' and name match end with 'ijk' var start = 'abc[\\s\\S]*'; var cont = '[\\s\\S]*efg[\\s\\S]*'; var end = '[\\s\\S]*ijk'; db.getCollection().find({ "$and":[ { "name":{"$regex":new RegExp('^' + start + '$', 'i')} }, { "name":{"$regex":new RegExp('^' + cont + '$', 'i')} }, { "name":{"$regex":new RegExp('^' + end + '$', 'i')} }] }) This runs very slow need some better query or

Remove array entries containing an empty array

邮差的信 提交于 2019-12-12 04:07:23
问题 I am trying to remove all products from the products array where the product has no rates. In my queries below I tried to check the length of the rates array, but none of them seem to work. Any help is appreciated. Thanks in advance var ProductRateSchema = new Schema({ product: { type: Schema.ObjectId, ref: 'products' }, user: { type: Schema.ObjectId, ref: 'User' }, rates: [{ type: Schema.ObjectId, ref: 'rates' }] }); var InventorySchema = new Schema({ name: { type: String, default: '',

Incorrect document revision number found in post update middleware

自闭症网瘾萝莉.ら 提交于 2019-12-12 04:05:49
问题 I have a Mongoose plugin which I use to increment the documents revision number ( __v ), as well as create revision itself. The plugin covers the Documents Doc.save() middleware function, as well as the Query update and findOneAndUpdate middleware functions. module.exports = ( schema, options ) => { _.forEach( [ 'save','update', 'findOneAndUpdate' ], query => { // Since the Doc.save() middleware is the only document update middleware function, just isolate that one if( query === 'save' )

How to find match elements in between two collections in mongodb?

假装没事ソ 提交于 2019-12-12 03:36:27
问题 I am working on mongodb database, but i am little stuck in one logic, how do i find match elements in between two collections in mongodb. Users Collection [{ "_id": "57cd539d168df87ae2695543", "userid": "3658975589", "name": "Anshuman Pattnaik", "email": "anshuman@gmail.com", "number": "7022650603" }, { "_id": "57cd53e6168df87ae2695544", "userid": "789456123", "name": "Deeptiman Pattnaik", "email": "deeptiman@gmail.com", "number": "7760941502" }] Contacts Collection [{ "_id":

How to write this MongoDb update operator

给你一囗甜甜゛ 提交于 2019-12-12 03:34:13
问题 I have an 'applications' collection in the following form: { "_id" : "<id>", "versions" : { "2_2_1" : 5, "2_2_2" : 38, "2_2_3" : 76 } } I want to increment the version number based on the version of application that sends a request. How can I write the $inc operator for my update query? I can't seem to get my query working because the incremented version number can always vary from request to request. I can't get the positional $ operator working correctly for my query. 回答1: You use "dot

How to get count from aggregate lookup results [duplicate]

岁酱吖の 提交于 2019-12-12 03:33:53
问题 This question already has answers here : Get a count of total documents with MongoDB when using limit (10 answers) Closed 3 years ago . I am looking to find out the total documents count from aggregate join. is it possible to get count from the below code? if anybody have an idea please let me know. db.collection("camp", function (err, camp) { camp.aggregate([{$match: {status: {$ne: "Rejected"}}}, {'$lookup': { from: "organization", localField: "organization", foreignField: "organizationId",

C# world has problems retrieving valid data from javaScript function that is gets data from MongoDB

此生再无相见时 提交于 2019-12-12 03:31:17
问题 Here is the information about my development environment: MongoDB 3.0.0 MongoDB C# Driver Version 1.7.0.4714 Microsoft Visual Studio Professional 2013 .NET Framework 4.0 We have code that uses C# MongoDB Driver API to invoke a JavaScript file that will query the MongoDB database. Here is the excerpt of the said C# code: var sysJs = DBConnection.database.GetCollection("system.js"); sysJs.Remove(Query.EQ("_id", "getArray")); var code = File.ReadAllText(HttpContext.Current.Server.MapPath("~

Get the count of Fields in each document through query using MongoDB java driver

时光毁灭记忆、已成空白 提交于 2019-12-12 03:25:08
问题 Is it possible to get the number of fields in document using a Query From MongoDB java Driver Example: Document1 :{_id:1,"type1": 10 "type2":30,"ABC":123,"DEF":345} Document2 :{_id:2,"type2":30,"ABC":123,"DEF":345} Note: In second document "type1" key doesnt exist . When i project Is it possible to project only "type1" and "type2" and get number of fields existing in that document. With current code i am getting all the documents and individually searching if there is the key i am looking is

Authentication on MongoDB 3.0.5 with Java Driver 3.0.3 and GridFS

元气小坏坏 提交于 2019-12-12 03:18:37
问题 I am trying to connect from Java Driver 3.0.3 with the connection string below to a Mongo 3.0.5: mongodb://admin:pass@myIP:myPort/databasename?authSource=databasename but I am getting the following exception: com.mongodb.MongoTimeoutException: Timed out after 30000 ms while waiting for a server that matches PrimaryServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=myIP:myPort, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoException: java.lang