In mongodb, I want to find a document which exist a field returned by a function.
for example:
var field = func()
var cur = db.test.find({field:{$exists:1}}).limit(1)
Mongodb to find a document has a field named "field", but I want to find a document has a field named the value of the var "field". what shoud I do. Thankd you very much.
Use this
db.CollectionName.find({},{FieldName:1})
in case I want to have results of students who have field named age in their document.
db.CollectionName.find({},{age:1}) // will result all documents with age in their field
Related
I want to update all the fields in a MongoDB document and I have a Javascript object that contains all these fields. I could easily type out each field to update but this seems like a lot of manual work and not reusable. I wanted to do something like below but this creates an object containing all the new field data within the document called newData.
I've tried JSON.stringify on the variable but the format isn't appropriate for update.
var newData = {
_id:ObjectId("53245234..."),
id: 88888,
firstData: "someData",
secondData: 787855,
thirdData: [ 45,17,12,234]
};
var collection = db.get('CollectionToUpdate');
//strip out dB id so as not to overwrite it, possibly not needed
if ("_id" in newData) {
delete newData["_id"];
}
//find the correct document based on program generated id and update
collection.update({id: newData.id}, {
newData
})
If you trust newData will not have any keys you don't intend (like update operators) this should work:
var collection = db.get('CollectionToUpdate');
collection.update({id: newData.id}, newData)
Note that this replaces the document. I assume that is what you meant by "update all the fields". update does not replace "_id".
Documentation for update
I have some documents with key / value pairs, now, I would like to create a second document, that has as its identifier (or self property) the name of some property of that second document.
Example:
Original Document:
Country = {
code: "ger",
lang: "de",
someArr: [...]
}
now, I would like to create a second document with a certain format so I can fit it in a chart (e.g ngx charts):
var ger = {lang: "de",...}
as you can see, that document has its variable name (var "ger") the name of the Country code, is it possible to pass dynamically the name of the Country code field as variable name for all documents? So if I have different countries in the array of documents, each time when I loop through it, I generate another document with its variable name the same as that document country code?
or would I have to create only anonymous documents with index fields and set the country code as a key field only?
I have a form with a lot of inputs and values. I want to push all the data to a firebase database. I could assign variables to each field value as:
let name = $("#field1").val();
let surname = $("#field2").val();
etc etc, but this feels very inefficient.
I want to create a object and loop through all input fields and map their name and value, something like this:
const collection = {};
$('form input').each(function () {
collection[$(this).attr('name')] = $(this).val();
});
Then I want to push all the data to firebase. But how do I push the entire object to firebase?
That's as simple as:
firebase.database().ref().push(collection);
Of if you want it under a specific location/path in the database:
firebase.database().ref("specific/place").push(collection);
Btw: I highly recommend reading the Firebase documentation on reading/writing lists of data.
When using Parse, if I have an object called people and two columns, one called name and another called age. The user can input a name to match one that is already stored in Parse, and also input an age to be added to that specific name. How can I get it to search for the name the user has inputted, and if the name matches the user's input, add the age to that specific name?
You cannot save anything in an object unless you have access to its objectId, therefore you need to perform a search as well as a save. You need to find the object(s) associated with the name the user enters and then add age value and save it. The code becomes something like:
var query = new Parse.Query("people");
query.equalTo("name", inputName);
query.find().then( function(objects) { // query and search for object(s)
var person = objects[0]; // get the first object, there can be more than one obviously
person.set("age", inputAge); // set the age
return person.save(); // save the age value in object
}).then(function() {
// success
}, function(error) {
// error
});
I'm trying to get list items from a SharePoint list with specific fields only.
The list contains lookup fields to other lists and I need the LookupId only for these.
I know two cases to narrow down the returned fields.
The first is with CAML. I can use the LookupId="TRUE" option to suppress the LookupValue in this case, but the server returns other system fields (eg. Modified By, Created By, etc.) which I don't need to.
The second is using 'Include' in ClientContext.load method. In this case the server not sends the system fields but I don't know how to specify (if possible) to receive the LookupId only for lookup fields.
I tried several versions in Include (project.id, projectId, project.lookupId, project.Inlude(id)) but none of them worked.
So, my question is: how to retrieve only specified fields of a SharePoint list with LookupIds only for lookup fields?
You were very close to archive the desired result, you need to combine two techniques in order to retrieve id part of lookup value as demonstrated below:
var ctx = SP.ClientContext.get_current();
var list = ctx.get_web().get_lists().getByTitle(listTitle);
var items = list.getItems(createLookupQuery('Project'));
ctx.load(items,'Include(Project)');
ctx.executeQueryAsync(
function(){
if(items.get_count() > 0){
var item = items.getItemAtIndex(0);
console.log(item.get_fieldValues()['Project'].get_lookupId());
}
},
function(sender,args)
{
console.log(args.get_message());
});
function createLookupQuery(lookupFieldName){
var qry = new SP.CamlQuery();
qry.set_viewXml("<View><ViewFields><FieldRef Name='" + lookupFieldName + "' LookupId='TRUE' /></ViewFields></View>");
return qry;
}
In CAML query we specify attribute LookupId='TRUE' to retrieve Id
part of lookup field value
since CAML query includes also system field values, we utilize
SP.ClientContext.load method to return specific field value
only
how to retrieve only specified fields of a SharePoint list with
LookupIds only for lookup fields?
The question is a bit unclear, but assuming you want to fetch the lookup id, you can do as follows.
Suppose you've retrieved the SP.ListItem with JSOM and stored it in to a variable, your lookup field will be representeed by a SP.LookupFieldValue object.
You can access it from the retrieved item object as below.
var lookupFieldValue = item.get_item('FieldName');
var lookupId = lookupFieldValue.get_lookupId();