mongodb-query

Mongoose find returning odd object

心已入冬 提交于 2019-12-20 02:28:25
问题 my current problem is with the db.collection.find() mongoose command. I'm relatively new to mongoose/mongodb, but I've gotten the hang of the concepts of it. Here is the test code I've been trying to run: mongoose.connect(url); function main() { var db = mongoose.connection; db.on('open', function() { db.collection('Tweet').find({id: 631460910368956400}, function (err, data){ console.log(data); }) /*var coll = db.collection('Tweet'); db.collection('Tweet').findOne({id: 631460910368956400}

Find and remove all documents whose “createdDate” is one month older

二次信任 提交于 2019-12-20 01:58:19
问题 I am working on MongoDB Collections . I have a set of collections each having many documents. I have to write a procedure to delete all those documents whose "createdDate" is older than one month from current date. I am stuck in finding the date which is one month back of the current date in order to compare it with document's "createdDate". Also i have to schedule this procedure, so that it can run automatically everyday. (OS is windows). How can i achieve it? 回答1: You could try getting a

Reverse array field in MongoDB

懵懂的女人 提交于 2019-12-20 01:09:21
问题 I have a collection with a location field that was entered in the wrong order: location: [38.7633698, -121.2697997] When I try to place a 2d index on the field using ... db.collection.ensureIndex({'location': '2d'}); ... I get the following error because the latitude and longitude are reversed. "err" : "location object expected, location array not in correct format", "code" : 13654 How can I reverse this array for each document in the mongo shell? 回答1: db.loc.find().forEach(function (doc) {

How to do alphanumeric sort in mongoDB?

只愿长相守 提交于 2019-12-19 18:55:31
问题 I have a collection like this. {"userID" : "TR31"} {"userID" : "TR1059"} {"userID" : "TR1043"} I want to sort this document in an ascending or descending order, I tried this way db.col.find({}).sort({"userID" : 1}) and db.col.find({}).sort({"userID" : -1}) but no luck. Expected Result: {"userID" : "TR31"} {"userID" : "TR1043"} {"userID" : "TR1059"} Please advise. Thanks in advance. 回答1: Use collation with numericOrdering set to true in 3.4. Something like db.col.find({}).sort({"userID" : 1})

Query Comparing Arrays of Objects

只愿长相守 提交于 2019-12-19 11:37:11
问题 I'm wondering how I can compare arrays of (nested) objects in Mongoose. Considering the data below, I would like to get results when the name properties match. Could anyone help me with this? Organisation.find( { $or: [ { "category_list": { $in: cat_list } }, { "place_topics.data": { $in: place_tops } } ] } ) Let's say that this is the data stored in my MongoDB: "category_list": [ { "id": "197750126917541", "name": "Pool & Billiard Hall" }, { "id": "197871390225897", "name": "Cafe" }, { "id":

I want to retrieve values inserted on particular date using _id of mongodb

无人久伴 提交于 2019-12-19 11:23:50
问题 I want to retrieve values inserted on particular date. Is this possible using mongodb "_id" field? as this contains embedded date time. I want to retieve values in mongodb shell not by using any application. 回答1: While it is true that the ObjectId is based on a "timestamp" in part, generally this is a "client" library operation to "extract" this date from the ObjectId value. You can do this with the JavaScript evaluation of $where, but it will need to "scan" the entire collection, so is not

Mongodb database Schema Design with shared data

孤者浪人 提交于 2019-12-19 10:33:32
问题 Hi I am newbie to mongodb.I am using java. I have 4 tables Tenant,system,authorization in my relational table. Something like this. Table Fields Tenant Tenant_ID(PK), Tenant_INFO System System_ID(PK), System_Info Authorization System_ID, Autho_Info. System_prop System_ID, Prop_Info, Tenant_ID In System_prop table, Tenant_ID refers the Tenant Table Tenant_ID (PK), System_ID refers the System Table System_ID. In Authorization table, System_ID refers System tabel System_ID I am switching my

MongoDb C# Typed Aggregations with Group Unwind and Project

爱⌒轻易说出口 提交于 2019-12-19 09:51:34
问题 I have a collection like this: [{ "_id": 1, "OtherProperties": 100 "PersonInventory": [{ "FirstName": "Joe", "MiddleName": "Bob", "LastName": "Blogs", "PersonId": 1 }] },{ "_id": 2, "OtherProperties": 1005 "PersonInventory": [{ "FirstName": "Joe", "MiddleName": "Bob", "LastName": "Blogs", "PersonId": 1 }] }] And I am trying to select all the unique persons in the root docs using here newer type inference mongodb c# driver syntax. Tried this so far but getting errors saying the method is not

Zip arrays with MongoDB

Deadly 提交于 2019-12-19 09:33:50
问题 Is it possible to zip arrays within a mongo document? I mean the functional programming definition of zip, where corresponding items are paired into tuples. To be more precise, I would like start with a mongo document like this: { "A" : ["A1", "A2", "A3"], "B" : ["B1", "B2", "B3"], "C" : [100.0, 200.0, 300.0] } and end up with mongo documents like these: {"A":"A1","B":"B1","C":100.0}, {"A":"A2","B":"B2","C":200.0}, {"A":"A3","B":"B3","C":300.0}, Ideally this would use the aggregation

How to ORDER BY FIELD VALUE in MongoDB

三世轮回 提交于 2019-12-19 09:28:45
问题 In Mysql I often use the FIELD() function in the ORDER BY clause: ORDER BY FIElD(id, '1', '6', '3', ...); How does one get the same results in MongoDB? I tried the following: .find(...).sort({id: [1, 6, 3]}) This did not work 回答1: So for the record: Given the array [1,6,3] what you want in your query is this: db.collection.aggregate([ { "$project": { "weight": { "$cond": [ { "$eq": ["_id": 1] }, 3, { "$cond": [ { "$eq": ["_id": 6] }, 2, { "$cond": [ { "$eq": ["_id": 3] }, 1, 0 ]}, ]}, ]} }},